简体   繁体   中英

Android JSON null pointer Exception while parsing

I am beginner to android .I am making one application related to webservice.I am getting json by hitting url and I want to show in listview .I tried so many ways but no result.I got json exceptions.please help me Here is my json:

    [
    [
    {
    "id":"9637",
    "country":"Australia",
    "time":"14:00",
    "type":"country",
    "status":"good"
    },
    {
    "id":"9638",
    "country":"india",
    "time":"16:00",
    "type":"country",
    "status":"good"
    }
    ]
    ]
code:
class Response extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {

            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            try {
                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
                jsonResponse = EntityUtils.toString(httpEntity);

            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return null;
        }

protected void onPostExecute(Void result) {

            Toast.makeText(MainActivity.this,jsonResponse,Toast.LENGTH_SHORT).show();
            try {
                JSONArray jsonArray=new JSONArray(jsonResponse);
                for(int i=0;i<jsonArray.length();i++)
                {
                    Toast.makeText(MainActivity.this,""+jsonArray.length(),Toast.LENGTH_SHORT).show();

                }
            //  JSONArray jsonArray=jsonObject.get


            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }



            super.onPostExecute(result);
        }

I think this code is correct:

        try {
            JSONArray json=new JSONArray(jsonResponse);
            JSONArray jsonArray=json.getJSONArray(0);
            for(int i=0;i<jsonArray.length();i++)
            {
                Toast.makeText(MainActivity.this,""+jsonArray.length(),Toast.LENGTH_SHORT).show();

            }
        //  JSONArray jsonArray=jsonObject.get


        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

because your JSON Response is an array of arrays.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM