当我执行这个异步任务时,我得到一个networkOnmainThreadException:
class GetGroupBiography extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(BiographyActivity.this);
pDialog.setMessage("Loading biography details. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Getting product details in background thread
* */
protected String doInBackground(String... params) {
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
// Check for success tag
int success;
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("nombre_grupo", nombre_grupo));
// getting product details by making HTTP request
// Note that product details url will use GET request
JSONObject json = jsonParser.makeHttpRequest(get_biography_url, "GET", params);
// check your log for json response
Log.d("Biography Details", json.toString());
// json success tag
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully received product details
JSONArray groupObj = json
.getJSONArray(TAG_GROUP); // JSON Array
// get first product object from JSON Array
JSONObject group = groupObj.getJSONObject(0);
// product with this pid found
// Edit Text
textName = (EditText) findViewById(R.id.textName);
textAnyo = (EditText) findViewById(R.id.textDate);
textDescripcion = (EditText) findViewById(R.id.textDescription);
// display product data in EditText
textName.setText(group.getString(TAG_NAME));
textAnyo.setText(group.getString(TAG_ANYO));
textDescripcion.setText(group.getString(TAG_DESCRIPTION));
}else{
// product with pid not found
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String result) {
// dismiss the dialog once got all details
super.onPostExecute(result);
if (pDialog.isShowing()) {
pDialog.dismiss();
// progressDialog.setCancelable(true);
}
}
}
我从此代码部分调用此异步任务:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_biography);
AlertDialog.Builder ald = new AlertDialog.Builder(BiographyActivity.this);
ald.setTitle("Nombre del grupo");
ald.setMessage("Introduce el nombre del grupo:");
final EditText texto = new EditText(BiographyActivity.this);
ald.setView(texto);
ald.setPositiveButton("Aceptar", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
nombre_grupo = texto.getText().toString();
new GetGroupBiography().execute();
}
});
ald.show();
}
我必须做些什么来避免这种异常。因为我试图将json移动到onpostexecute()但它没有用。我希望能帮助你完成这项任务,因为我必须尽快完成这项任务。
答案 0 :(得分:1)
我必须采取哪些措施来避免此异常
摆脱runOnUiThread()
。
然后,在doInBackground()
中执行JSON解析,并将UI更新逻辑(例如,setText()
调用)移至onPostExecute()
。
答案 1 :(得分:0)
您无法在UI线程上执行网络操作,然后明确要求UI线程执行这些操作
删除runOnuiThread并将其保留在DoInBackground。
把它放在postexecute上:
textName = (EditText) findViewById(R.id.textName);
textAnyo = (EditText) findViewById(R.id.textDate);
textDescripcion = (EditText) findViewById(R.id.textDescription);
// display product data in EditText
textName.setText(group.getString(TAG_NAME));
textAnyo.setText(group.getString(TAG_ANYO));
textDescripcion.setText(group.getString(TAG_DESCRIPTION));