本文介绍了Java BufferedReader值变为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用以下代码来获取API的响应。
I'm using following code to get response from the API.
BufferedReader bf = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
System.out.println("bf.readLine() - " + bf.readLine());
output = bf.readLine();
while (output != null) {
JSONObject obj = new JSONObject(output);
System.out.println("output is " + output);
resCode = obj.getString("resCode");
resDesc = obj.getString("COUNT");
}
我可以按以下方式返回bf.readLine()响应。
I can return bf.readLine() response as follows.
{"status":true,"data":[{"COUNT":"0"}]}
问题是当我将 bf.readLine()
分配给String并检查值,它变为空。为什么bf.readLine()即使从API返回值也显示为空(给出空点异常)。
Problem is when I assign the bf.readLine()
to String and check the value, it becomes null. Why bf.readLine() shows as null (gives null point exception) even it return the value from API.
推荐答案
您的代码应该是
BufferedReader bf = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String output = null;
while ((output = bf.readLine()) != null) {
System.out.println("bf.readLine() value is--- - " + output );
JSONObject obj = new JSONObject(output);
System.out.println("output is " + output);
resCode = obj.getString("resCode");
resDesc = obj.getString("COUNT");
}
这篇关于Java BufferedReader值变为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!