Reputation: 369
In my code, I am trying to access some json return from server. But I can't convert json return from server to JsonObject. Here is my code,
result = post.getHttpData(Constants.UrlDepartureCity);
catchLog(result);
JSONObject obj=new JSONObject(result);
I use my custom catchLog to print json return in log.
09-23 14:53:59.940: I/DepartureCityAsync(23313):
{"status":"1","departure_city":[
{"entity_id":"1","field_depature_city_tid":"1","entity_type":"node"},
{"entity_id":"8","field_depature_city_tid":"1","entity_type":"node"},
{"entity_id":"12","field_depature_city_tid":"1","entity_type":"node"},
{"entity_id":"5","field_depature_city_tid":"2","entity_type":"node"},
{"entity_id":"9","field_depature_city_tid":"2","entity_type":"node"},
{"entity_id":"17","field_depature_city_tid":"2","entity_type":"node"},
{"entity_id":"6","field_depature_city_tid":"3","entity_type":"node"},
{"entity_id":"7","field_depature_city_tid":"5","entity_type":"node"}]}
And I copy the result and validate that json return in http://jsonlint.com/. There is no error, syntax also correct. But When I try to convert it to JsonObject I got this error.
09-23 14:53:59.940: W/System.err(23313): org.json.JSONException: Value of type java.lang.String cannot be converted to JSONObject
How can I solve this problem? Please. Thanks
Upvotes: 0
Views: 282
Reputation: 1768
final TextView res=(TextView)findViewById(R.id.res);
try
{
HttpClient client=new DefaultHttpClient();
HttpPost request=new HttpPost("http://192.168.0.30/test.js");
HttpResponse response=client.execute(request);
HttpEntity entity=response.getEntity();
res.setText(EntityUtils.toString(entity));
}
catch(Exception e)
{
res.setText(e.toString());
}
here is my code sample it works for me...for the same json as you provided. can you show me your code how you get the response.i didn't get the method getHttpData() you've used.
Upvotes: 0
Reputation: 2969
I think your json format is wrong or has some little mistake. From this link, you can find a solution.
Upvotes: 0
Reputation: 241
I think there may be some blank characters in your return. Change it to JSONObject obj=new JSONObject(result.trim());
Upvotes: 1