Null Pointer Exception while retrieving JSON object

前端 未结 3 756
既然无缘
既然无缘 2021-01-24 05:35

I am new to JSON. I am using http://pnrapi.appspot.com/ to get the status of a particular train using JSON. But while trying to parse the received object i always get a null poi

3条回答
  •  遥遥无期
    2021-01-24 06:04

    use get instead of getString as:

    try {
        JSONObject jsona=new JSONObject("{'status': 'INVALID', 'data': 'No results'}");
        String id = (String)jsona.get("status");
        Toast.makeText(DfgdgdfgdfActivity.this, id, Toast.LENGTH_LONG).show();
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    

    EDIT:

    use

    while ((line = reader.readLine()) != null) {
                    sb.append(line);
                }
    

    instead of

    while ((line = reader.readLine()) != null) {
                    sb.append(line + "n");
                }
    

提交回复
热议问题