-1

While executing Async Task in android and getting Json response and while converting response into JSONArray,i am getting NUll pointer Exception. I am trying fron two days Please help me. Here is the code to get the Json String. error is at task.get().

DownloadTask task=new DownloadTask();
        task.execute(new String[]{"URL"});
        try {
            jsonArr=new JSONArray(task.get());
                Toast.makeText(getApplicationContext(), jsonArr.toString(), Toast.LENGTH_LONG).show();
                for (int i = 0; i < jsonArr.length(); i++) {
                    obj = jsonArr.getJSONObject(i);
                    name = obj.getString("name");
                    phno = obj.getString("phone");
                    dcount = obj.getString("count");
                }
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

Here Is the Async task code.

class DownloadTask extends AsyncTask<String,Void,String>{

        private ProgressDialog mProgressDialog=new ProgressDialog(MainActivity.this);

        @Override
        protected void onPreExecute(){
            mProgressDialog.setMessage("Processing");
            mProgressDialog.show();
        }

        @Override
        protected String doInBackground(String... targetURL) {
            URL url;
            HttpURLConnection connection = null;
            try {

                url = new URL(targetURL[0]);
                connection = (HttpURLConnection)url.openConnection();

                connection.setRequestMethod("GET");
                connection.setRequestProperty("Content-Type",
                        "application/json");

                connection.setUseCaches (false);
                connection.setDoInput(true);
                connection.setDoOutput(true);

             /*   //Send request
                DataOutputStream wr = new DataOutputStream (
                        connection.getOutputStream ());
                wr.writeBytes("BID1");
                wr.flush();
                wr.close();*/

                //Get Response
                InputStream is = connection.getInputStream();
                BufferedReader rd = new BufferedReader(new InputStreamReader(is));
                String line;
                StringBuffer response = new StringBuffer();
                while((line = rd.readLine()) != null) {
                    response.append(line);
                    response.append('\r');
                }
                rd.close();
                return response.toString();

            } catch (Exception e) {
                e.printStackTrace();
                return null;

            } finally {

                if(connection != null) {
                    connection.disconnect();
                }
            }
        }
        @Override
        protected void onPostExecute(String result) {

            Toast.makeText(getApplicationContext(),result, Toast.LENGTH_SHORT);
            mProgressDialog.dismiss();

        }
    }

1 Answers1

0

You are forgot to call the super method of the onPostExecute

It should be like this

@Override
protected void onPostExecute(String result) {

    Toast.makeText(getApplicationContext(),result, Toast.LENGTH_SHORT);
    mProgressDialog.dismiss();
    super.onPostExecute(result);
}

Other Solution

You can use an interface for your callback

ICallback.java

public interface ICallback {
    void onResult(String result);
}

DownloadTask

class DownloadTask extends AsyncTask<String, Void, String> {
    private ProgressDialog mProgressDialog = new ProgressDialog(MainActivity.this);
    private ICallback callback;

    public DownloadTask(ICallback callback) {
        this.callback = callback;
    }

    @Override
    protected void onPreExecute() {
        //Your Codes Here
    }

    @Override
    protected String doInBackground(String... targetURL) {
        //Your Codes Here
    }

    @Override
    protected void onPostExecute(String result) {
        //Your Codes Here
        callback.onResult(result)
    }
}

How to use it

DownloadTask task = new DownloadTask(new ICallback() {
    @Override
    public void onResult(String result) {
        try {
            jsonArr=new JSONArray(result);
            Toast.makeText(getApplicationContext(), jsonArr.toString(), Toast.LENGTH_LONG).show();
            for (int i = 0; i < jsonArr.length(); i++) {
                obj = jsonArr.getJSONObject(i);
                name = obj.getString("name");
                phno = obj.getString("phone");
                dcount = obj.getString("count");
            }
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}); 

task.execute(new String[]{"URL"});
NaviRamyle
  • 3,827
  • 1
  • 28
  • 49