Reputation: 17
Here is the response that I am receiving from my REST service from a GET Request:
{
"type": "usersTable",
"employeeId": 1,
"employeeName": "Andrew Watson",
"userPin": "705207"
}
I am trying to retrieve the userPin
value from the JSON.
Here is the code I have attempted:
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"));
}
}
Can anyone see why I am getting a Null Pointer?
Upvotes: 0
Views: 595
Reputation: 1789
I have not tested this one yet. But I think the problem is userPin
is string not an integer value.
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());
if(json.getString("userPin") == null) {
//do something here or define the default value
}else {
userPinRetrieved = json.getString("userPin");
}
}
}
Upvotes: 1