我已经尝试过解析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>
答案 0 :(得分: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
}
答案 1 :(得分: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)的构造函数,并在需要访问调用活动方法的任何位置使用此上下文。