在使用Okhttp中,获取json数据时,发现获取的数据不完全。
服务器文件
使用okhttp请求test2.json(59KB)
···
OkHttpClient client = new OkHttpClient();
Request request=new Request.Builder()
.get()
.url("http://192.168.31.207/test2.json")
.build();
client.newCall(request).enqueue(new getCallBack());
···
getCallBack()为implements Callback(okhttp包下的)的类
重写成功的回调方法
···
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()){
// String line ;
// BufferedReader br = new BufferedReader(new InputStreamReader(response.body().byteStream()));
// while ((line=br.readLine())!=null){
// L.e(line);
// }
Reader r = response.body().charStream();
char[] B = new char[4096];
int length;
while ((length=r.read(B))>0){
L.e(String.valueOf(B));
}
r.read(B);
}
}
···
无论是用byteStream还是用reader 都不能获取完整数据
把char数组换成更大100000+也无济于事
打印的数据残缺。
但是将请求换成test.html(254KB)时又能全部输出
请问如何能成功的获取json数据?
怪我咯2017-04-18 09:07:49
@Override
public void onResponse(Response response) {
if (response.isSuccessful()) {
try {
final String result = response.body().string();
if (!TextUtils.isEmpty(result)) {
JSONObject obj = new JSONObject(result);
// 解析資料
}
} catch (Exception e) {
Log.e(TAG, "Exception = " + e);
}
}
}
阿神2017-04-18 09:07:49
Through testing, we found that the data printed by the control line has a length limit, and not all output will be printed. But the data is complete.
高洛峰2017-04-18 09:07:49
Because the data printed at one time in the logcat of android studio has 4M (4*1024k) displayed, so if there is too much data in json, it will not be fully displayed in the logcat of android studio. If you want to see all the logs, You need to export the log to view it. Here is a recommended way to use notepad++ to view: https://zhuanlan.zhihu.com/p/...