【发布时间】:2018-06-01 09:31:34
【问题描述】:
这是我从 REST 服务收到的来自 GET 请求的响应:
{
"type": "usersTable",
"employeeId": 1,
"employeeName": "Andrew Watson",
"userPin": "705207"
}
我正在尝试从 JSON 中检索 userPin 值。
这是我尝试过的代码:
if (con.getResponseCode() == HttpsURLConnection.HTTP_OK
|| con.getResponseCode() == HttpsURLConnection.HTTP_ACCEPTED ) {
String line;
BufferedReader br=new BufferedReader(new InputStreamReader(con.getInputStream()));
while ((line=br.readLine()) != null) {
JSONObject json = new JSONObject(br.toString());
userPinRetrieved = String.valueOf(json.getInt("userPin"));
}
}
谁能明白为什么我得到一个空指针?
【问题讨论】:
-
你能和我们分享一下错误日志吗?
-
为什么是
br.toString()? -
@Lokesh 我在本网站的其他示例中看到过这种方式。我应该使用其他方式吗?
-
请完整的崩溃日志。
-
json.getInt在字符串上?您的userPin是 String 而不是 int。
标签: java android json parsing httpresponse