android - OkHttp庫使用如何完整的獲取json數(shù)據(jù)
問題描述
在使用Okhttp中,獲取json數(shù)據(jù)時(shí),發(fā)現(xiàn)獲取的數(shù)據(jù)不完全。服務(wù)器文件
使用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包下的)的類
重寫成功的回調(diào)方法···
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 都不能獲取完整數(shù)據(jù)把char數(shù)組換成更大100000+也無濟(jì)于事
打印的數(shù)據(jù)殘缺。
但是將請求換成test.html(254KB)時(shí)又能全部輸出
請問如何能成功的獲取json數(shù)據(jù)?
問題解答
回答1:@Overridepublic 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);} }}回答2:
通過測試發(fā)現(xiàn),控制行打印的數(shù)據(jù)有長度限制,并不會將所有的輸出進(jìn)行打印。但是數(shù)據(jù)是完整的。
回答3:因?yàn)閍ndroid studio的logcat中一次打印的數(shù)據(jù)有4M(4*1024k)的顯示,所以,如果json中的數(shù)據(jù)太多,在android studio的logcat中是顯示不完全的,如果想款看全部日志,需要把日志導(dǎo)出來查看。這里推薦一種使用notepad++查看的方式:https://zhuanlan.zhihu.com/p/...
相關(guān)文章:
1. apache web server 怎么限制某一個(gè)網(wǎng)站對服務(wù)器資源的占用?2. docker網(wǎng)絡(luò)端口映射,沒有方便點(diǎn)的操作方法么?3. python - pandas dataframe如何對某列的空數(shù)據(jù)位置進(jìn)行update?update的函數(shù)是自定義的,參數(shù)是同一行的另外兩列數(shù)據(jù)4. docker start -a dockername 老是卡住,什么情況?5. java中返回一個(gè)對象,和輸出對像的值,意義在哪兒6. javascript - 關(guān)于apply()與call()的問題7. css3 - 純css實(shí)現(xiàn)點(diǎn)擊特效8. javascript - jQuery post()方法,里面的請求串可以轉(zhuǎn)換為GBK編碼么?可以的話怎樣轉(zhuǎn)換?9. docker - 各位電腦上有多少個(gè)容器啊?容器一多,自己都搞混了,咋辦呢?10. 安全性測試 - nodejs中如何防m(xù)ySQL注入
