-1

I have tried that code for parsing url contain JSON objects but it there is an error at the line :private ProgressDialog progressDialog = new ProgressDialog(ActivitiesActivity.this);

this ActivitiesActivity to show the list from the json objects .

This is the code:

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

private ProgressDialog progressDialog = new ProgressDialog(ActivitiesActivity.this);
InputStream inputStream = null;
String result = ""; 

protected void onPreExecute() {
    progressDialog.setMessage("Downloading your data...");
    progressDialog.show();
    progressDialog.setOnCancelListener(new OnCancelListener() {
        public void onCancel(DialogInterface arg0) {
            MyAsyncTask.this.cancel(true);
        }
    });
}

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

    String url_select = "http://yoururlhere.com";

            ArrayList<NameValuePair> param = new ArrayList<NameValuePair>();

    try {
        // Set up HTTP post

        // HttpClient is more then less deprecated. Need to change to URLConnection
        HttpClient httpClient = new DefaultHttpClient();

        HttpPost httpPost = new HttpPost(url_select);
        httpPost.setEntity(new UrlEncodedFormEntity(param));
        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();

        // Read content & Log
        inputStream = httpEntity.getContent();
    } catch (UnsupportedEncodingException e1) {
        Log.e("UnsupportedEncodingException", e1.toString());
        e1.printStackTrace();
    } catch (ClientProtocolException e2) {
        Log.e("ClientProtocolException", e2.toString());
        e2.printStackTrace();
    } catch (IllegalStateException e3) {
        Log.e("IllegalStateException", e3.toString());
        e3.printStackTrace();
    } catch (IOException e4) {
        Log.e("IOException", e4.toString());
        e4.printStackTrace();
    }
    // Convert response to string using String Builder
    try {
        BufferedReader bReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"), 8);
        StringBuilder sBuilder = new StringBuilder();

        String line = null;
        while ((line = bReader.readLine()) != null) {
            sBuilder.append(line + "\n");
        }

        inputStream.close();
        result = sBuilder.toString();

    } catch (Exception e) {
        Log.e("StringBuilding & BufferedReader", "Error converting result " + e.toString());
    }
} // protected Void doInBackground(String... params)


protected void onPostExecute(Void v) {

    //parse JSON data
    try{
        JSONArray jArray = new JSONArray(result);

        for(int i=0; i < jArray.length(); i++) {

            JSONObject jObject = jArray.getJSONObject(i);

            String name = jObject.getString("Title");
            String tab1_text = jObject.getString("Date");
            //int active = jObject.getInt("active");


        } // End Loop

        this.progressDialog.dismiss();

    } catch (JSONException e) {

        Log.e("JSONException", "Error: " + e.toString());

    } // catch (JSONException e)


} // protected void onPostExecute(Void v)

} //class MyAsyncTask extends AsyncTask<String, String, Void>
user2870902
  • 81
  • 4
  • 13

2 Answers2

1

From your comments "no the asynctask not an inner class".

If your asynctask is not a inner class you need to pass the activity context to the constructor of Asynctask.

 new MyAsyncTask(YourActivityName.this).execute(params);

Then in AsyncTask have the constructor and initialize progressDialog

 private ProgressDialog progressDialog;
 public MyAsyncTask(Context context)
 {
     progressDialog = new ProgressDialog(context);
 }

Edit: add @Override Annotation and call super.onPreExecute() and super.onPostExecute(param)

@Override
protected void onPostExecute(Void V) {
    // TODO Auto-generated method stub
    super.onPostExecute(v);
            // rest of the code
}

@Override
protected void onPreExecute() {
    // TODO Auto-generated method stub
    super.onPreExecute();
           // rest of the code
}
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
0

Update your AsyncTask like this

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

    Context context;

    private ProgressDialog progressDialog;
    InputStream inputStream = null;
    String result = ""; 


    public MyAsyncTask(Context context)
         {
            this.context=context;
         }

    protected void onPreExecute() {

    progressDialog=ProgressDialog.show(context, "", "Downloading your data...");
        progressDialog.setOnCancelListener(new OnCancelListener() {
            public void onCancel(DialogInterface arg0) {
                MyAsyncTask.this.cancel(true);
            }
        });
    }

Pass context to the constructor of your AsyncTask(MyAsyncTask ) and Use this context wherever you need to access Methods of the Calling Activity.

Jitender Dev
  • 6,907
  • 2
  • 24
  • 35