-1

URLを解析するためのコードにJSONオブジェクトが含まれていることを試しましたが、次の行にエラーがあります:private ProgressDialog progressDialog = new ProgressDialog(ActivitiesActivity.this);

この ActivitiesActivity は、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 アノテーションを追加して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 に答える