-1

我已经尝试过用于解析 url 的代码包含 JSON 对象,但是该行出现错误:private ProgressDialog progressDialog = new ProgressDialog(ActivitiesActivity.this);

此活动活动显示来自 json 对象的列表。

这是代码:

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>
4

2 回答 2

1

从您的评论中“没有 asynctask 不是内部类”。

如果您的 asynctask 不是内部类,则需要将活动上下文传递给 Asynctask 的构造函数。

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

然后在 AsyncTask 有构造函数并初始化progressDialog

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

编辑:添加@Override Annotation 并调用super.onPreExecute()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
}
于 2013-10-27T07:34:14.000 回答
0

像这样更新你的 AsyncTask

 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);
            }
        });
    }

将上下文传递给 AsyncTask(MyAsyncTask) 的构造函数,并在需要访问调用活动方法的任何地方使用此上下文。

于 2013-10-27T07:43:19.387 回答