Not Getting Complete Data From Json
Here I am trying to get Json data from URL. But oit is displaying only partial data. Below are the details how I am reading the data BufferedInputStream is; HttpGet httpGet = new H
Solution 1:
Try out below method:
/*
* Call the Webservice read the Json response and return the response in
* string.
*/publicstaticStringparseJSON(String p_url) {
JSONObject jsonObject = null;
String json = null;
try {
// Create a new HTTP ClientDefaultHttpClient defaultClient = newDefaultHttpClient();
// Setup the get requestHttpGet httpGetRequest = newHttpGet(p_url);
System.out.println("Request URL--->" + p_url);
// Execute the request in the clientHttpResponse httpResponse = defaultClient.execute(httpGetRequest);
// Grab the responseBufferedReader reader = newBufferedReader(newInputStreamReader(
httpResponse.getEntity().getContent(), "UTF-8"));
json = reader.readLine();
System.err.println("JSON Response--->" + json);
// Instantiate a JSON object from the request response
jsonObject = newJSONObject(json);
} catch (Exception e) {
// In your production code handle any errors and catch the// individual exceptions
e.printStackTrace();
}
return json;
}
Use above method to parse the json response as below:
String abcd = CommonUtils.parseJSON(url);
JSONObject jo = newJSONObject(abcd);
Solution 2:
Try this method to Read Response
public String getResponseBody(final HttpEntity entity)throws IOException, ParseException {
if (entity == null) {
thrownewIllegalArgumentException("HTTP entity may not be null");
}
InputStreaminstream= entity.getContent();
if (instream == null) {
return"";
}
if (entity.getContentLength() > Integer.MAX_VALUE) {
thrownewIllegalArgumentException(
"HTTP entity too large to be buffered in memory");
}
StringBuilderbuffer=newStringBuilder();
BufferedReaderreader=newBufferedReader(newInputStreamReader(instream, HTTP.UTF_8));
Stringline=null;
try {
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
} finally {
instream.close();
reader.close();
}
return buffer.toString();
}
How to use?
HttpGethttpGet=newHttpGet(url);
httpGet.addHeader("Accept", "application/json");
HttpResponsehttpResponse= getHttpClient().execute(httpGet);
HttpEntityhttpEntity= httpResponse.getEntity();
Stringresponse= getResponseBody(httpEntity);
try {
// Parse the data into jsonobject to get original data in form of// json.JSONObjectjObject=newJSONObject(
response);
jObj = jObject;
Log.v("JsonParser", "JsonByteArry data: " + jObj.toString());
} catch (Exception e) {
e.printStackTrace();
}
Post a Comment for "Not Getting Complete Data From Json"