A valid String cant be converted to JSONObject using volley - android

I am reading some inforamtion from database as json object .
the database returns a well formated string ( as json object ) .
When i try to convert the string to jsonObject .. it returns null
here is the string from database :
{"items":[{"title":"Police","icon":"police","type":"normal","tel":"999"},{"title":"Ambulance","icon":"ambulance","type":"normal","tel":"976"},{"title":"Electricity","icon":"electricity","type":"normal","tel":"4848"},{"title":"Fire Station","icon":"fire","type":"normal","tel":"998"},{"title":"Airport Information 1","icon":"airport","type":"normal","tel":"0183447405"},{"title":"Airport Information 2","icon":"airport","type":"normal","tel":"0183780129"},{"title":"Water","icon":"water","type":"normal","tel":"3131"}],"success":1}
and here the response error from logcat :
12-18 20:24:44.529: W/System.err(25062): org.json.JSONException: Value of type java.lang.String cannot be converted to JSONObject
as you can see it indicate that the string value is empty ... while the logcat shows me my string ....
Please note that this code was working very well before ..
EDIT :
I tried the same code to read from different database . and it worked well
i guess its database error .

Related

android JSON parsing result NULL jsonobject

i'm trying to create a list of objects from a JSON file but my result is always NULLenter image description hereenter image description here
Problem is on this line
JSONObject json = new JSONObject("listBeer.json");
You have pass you JSON string here, not the file name. See documentation here. Your code will thow JSONException, since string listBeer.json is not a valid JSON.

JSONException to parse HTML formatted text

I'm calling a web service from my Android application. The web service returns the following JSON :
Now the problem is when I'm trying to get the description element of the record object I'm getting an exception as org.json.JSONException: No value for description
Now my question is how can I get the value of the description using the existing parser without changing the response JSON format.
According to me,
You are using the wrong object to get description value otherwise org.json.JSONException: No value for description error will not occur.
Also, check if you can using optString instead of getString which just returns null if value do not exist, instead of throwing an exception.

org.json.JSONException: Value !DOCTYPE of type java.lang.String cannot be converted to JSONObject

I have to send HttpRequest to server using api in this manner.
json = jsonParser.makeHttpRequest(URL+userid+"key"+serverKey,"GET",params);
and I am getting this error.
org.json.JSONException: Value !DOCTYPE of type java.lang.String cannot be converted to JSONObject
But When I am sending userID statically like this..
json = jsonParser.makeHttpRequest(URL+123+"key"+favc56sjdlkht654hatakomn657g4328,"GET",params);
I am getting proper json response.
this is the api I am using.
http://m.sabakuch.com/api/mypost/id/1289/key/fa4cdfa057fdfc19e3470cd7b6ee943a
please let me know what mistake I am doing here??

Why do I get this error in my logcat when I try to post JSON data?

This is the code my Android application uses to post JSON data. When I execute it, I get the following error in my logcat. Why?
Try this.
Logcat says org.json.JSONException: No value for success
that means that you are trying to retrieve a value from the JSONObject using success that doesn't exist in the JSONObject
Use has(""); to check if key is present in Json
String success ="":
if (Jobj.has("success")) {
success = Jobj.getString("success");
}

JSONException: java.lang.String cannot be converted to JSONObject

I know this question has been asked a number of times but I didn't find any relative answer of my question.
I am trying to read json data from asset folder, but I'm getting following exception while getting
I searched number of stuffs but didn't help. Please give me any reference or hint.
Thanks in Advance.
Use
String searchedTerm = jsonObject.getString(TAG_SEARCHEDTERM);
JSONArray results = jsonObject.getJSONArray(TAG_RESULTS);
instead of
JSONObject searchedTerm = jsonObject.getJSONObject(TAG_SEARCHEDTERM);
JSONArray results = searchedTerm.getJSONArray(TAG_RESULTS);
because TAG_SEARCHEDTERM is key-value pair instead of JSONObject and you are trying to cast a String value to JsonObject.
I guess you need to get the Dish Name where you are getting exception.
You can get the dish name shown below...
String searchedTerm = jsonObject.getString(TAG_SEARCHEDTERM);
Using this
JSONArray results = jsonObject.getJSONArray(TAG_RESULTS);
you will get the "results" JSONArray as shown in your json file.
and you can iterate through it using for loop.

Categories

Resources