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 HttpGet(url);
httpGet.addHeader("Accept", "application/json");
HttpResponse httpResponse = getHttpClient().execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = new BufferedInputStream(httpEntity.getContent()) ;
public void getJsonwithByteArray(BufferedInputStream istream) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int ctr;
try {
ctr = istream.read();
while (ctr != -1) {
byteArrayOutputStream.write(ctr);
ctr = istream.read();
}
istream.close();
} catch (IOException e) {
e.printStackTrace();
}
Log.v("Text Data", byteArrayOutputStream.toString());
try {
// Parse the data into jsonobject to get original data in form of
// json.
JSONObject jObject = new JSONObject(
byteArrayOutputStream.toString());
jObj = jObject;
Log.v("JsonParser", "JsonByteArry data: " + jObj.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
Try this method to Read Response
public String getResponseBody(final HttpEntity entity) throws IOException, ParseException {
if (entity == null) {
throw new IllegalArgumentException("HTTP entity may not be null");
}
InputStream instream = entity.getContent();
if (instream == null) {
return "";
}
if (entity.getContentLength() > Integer.MAX_VALUE) {
throw new IllegalArgumentException(
"HTTP entity too large to be buffered in memory");
}
StringBuilder buffer = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(instream, HTTP.UTF_8));
String line = null;
try {
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
} finally {
instream.close();
reader.close();
}
return buffer.toString();
}
How to use?
HttpGet httpGet = new HttpGet(url);
httpGet.addHeader("Accept", "application/json");
HttpResponse httpResponse = getHttpClient().execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
String response = getResponseBody(httpEntity);
try {
// Parse the data into jsonobject to get original data in form of
// json.
JSONObject jObject = new JSONObject(
response);
jObj = jObject;
Log.v("JsonParser", "JsonByteArry data: " + jObj.toString());
} catch (Exception e) {
e.printStackTrace();
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다