Android的JSON接收问题 [英] Android JSON receive issue
本文介绍了Android的JSON接收问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个字符串的问题,我用下面的code从一个URL recieving JSON数据中,code工作正常,但问题是我没有得到完整的数据只有一半的JSON值来了,我想知道是否有原因,如果是指如何解决这个问题。 JSON字符串是非常大的。
DefaultHttpClient http_client =新DefaultHttpClient();
HTTPGET HTTPGET =新HTTPGET(网址[0]);
HTT presponse响应= http_client.execute(HTTPGET); 状态行状态行= response.getStatusLine();
INT状态code = statusLine.getStatus code();
HttpEntity实体= response.getEntity(); InputStream的时间= entity.getContent();
StringBuffer的出=新的StringBuffer();
字节[] B =新的字节[4096];
INT N = in.read(B);
而(N 0){
out.append(新的String(B,O,N));
N = in.read(B);
} 串resultdata = out.toString();
Log.d(出的数据,resultdata);
解决方案
尝试获得使用的BufferedReader这样的数据
串线=;
RD的BufferedReader =新的BufferedReader(新的InputStreamReader(中));
//读响应
而((行= rd.readLine())!= NULL){
total.append(线);
}
串jsonString = total.toString();
I have a string problem, I used the below code for recieving JSON data from an URL, the code is working fine, but the problem is I am not getting full data only half of the JSON values are coming, I would like to know whether there is reason for this, if so means how to solve this problem. JSON string is very big
DefaultHttpClient http_client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(urls[0]);
HttpResponse response = http_client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
HttpEntity entity = response.getEntity();
InputStream in = entity.getContent();
StringBuffer out = new StringBuffer();
byte[] b = new byte[4096];
int n = in.read(b);
while(n>0){
out.append(new String(b, 0, n));
n = in.read(b);
}
String resultdata = out.toString();
Log.d("Out data",resultdata);
解决方案
Try getting data like this using BufferedReader
String line="";
BufferedReader rd = new BufferedReader(new InputStreamReader(in));
// Read response
while ((line = rd.readLine()) != null) {
total.append(line);
}
String jsonString=total.toString();
这篇关于Android的JSON接收问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文