Reputation: 1580
I am getting response in this format (Whole response)
I have tried the following code so far but getting error
private String connect(String url) {
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);
HttpResponse response;
String returnString = null;
try {
response = httpclient.execute(httpget);
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
String res = convertStreamToString(instream);
JSONObject jsonObj = new JSONObject(res);
String f = jsonObj.getString("Result");
f = f.trim();
System.out.println("!!!!!!!!!!!!!!! "+f);
String s= jsonObj.getString("About");
System.out.println("@@@@@@ "+s);
JSONArray get = jsonObj.getJSONArray("Result");
// lets loop through the JSONArray and get all the items
for (int i = 0; i < get.length(); i++) {
// printing the values to the logcat
System.out.println("&&&&&&&&&&"+get.getJSONObject(i).getString("About").toString());
System.out.println("&&&&&&&&&&"+get.getJSONObject(i).getString("AboutMeText").toString());
}
instream.close();
}
} else {
returnString = "Unable to load page - "
+ response.getStatusLine();
}
} catch (IOException ex) {
returnString = "Connection failed; " + ex.getMessage();
} catch (JSONException ex) {
returnString = "JSON failed; " + ex.getMessage();
}
return returnString;
}
private static String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
Everytime i am trying to parse it it gives me json failed exception and says no value for.. Please let me know if i am making any mistake here.
Upvotes: 0
Views: 212
Reputation: 7814
Much simpler way is to use GSON. (all code from memory so...)
for {Result: ["{"About":"","AboutMeText":[{}]]}
I'd create a class
public class Result {
private String About;
private String AboutMeText;
//getters and setters
}
Then in order to deserialize
Gson gson = new Gson();
Result result = gson.fromJson(jsonString, Result.class);
Upvotes: 0
Reputation: 1085
The JSON is not valid do you want this?
{
"Result": [
{
"About": "",
"AboutMeText": {}
}
]
}
Upvotes: 1