执行JSON查询时出现AndroidLockGuardPolicy错误
我在尝试执行JSON查询时遇到AndroidLockGuardPolicy错误。看看其他的例子,我想使用AsyncTask,我想我已经这么做了,但是我得到了一个错误。。。我只是没看到什么吗执行JSON查询时出现AndroidLockGuardPolicy错误,android,json,android-asynctask,Android,Json,Android Asynctask,我在尝试执行JSON查询时遇到AndroidLockGuardPolicy错误。看看其他的例子,我想使用AsyncTask,我想我已经这么做了,但是我得到了一个错误。。。我只是没看到什么吗 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.pm_article); context
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pm_article);
context = this;
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectAll()
.penaltyLog()
.penaltyDialog()
.build());
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
// getting product details from intent
Intent i = getIntent();
// getting product id -section id (sid) from intent
//Receive the section id (SID) from th intent
String id = i.getStringExtra(PM_section.INTENT_ID);
id_pub = id;
new GetProductDetails().execute();
}
异步任务
class GetProductDetails extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(PM_article.this);
pDialog.setMessage("Loading product details. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Getting product details in background thread
* */
protected String doInBackground(String... args) {
runOnUiThread(new Runnable() {
public void run () {
SharedPreferences sharedPrefs = PreferenceManager
.getDefaultSharedPreferences(context);
int success;
try {
//sid2.setText("ID: " + id_pub);
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("id", id_pub));
System.out.println("Loading stories into the activity!");
// getting product details by making HTTP request
// Note that product details url will use GET request
JSONObject json = jParser.makeHttpRequest(url_load_article, "GET", params);
System.out.println("Loading stories into the actisadfsafdsafvity!");
// check your log for json response
Log.d("Single Product Details", json.toString());
// json success tag
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully received product details
JSONArray contentObj = json
.getJSONArray(TAG_PRODUCT); // JSON Array
// get first product object from JSON Array
JSONObject product = contentObj.getJSONObject(0);
// product with this pid found
// Edit Text
art_title = (TextView) findViewById(R.id.display_title);
art_author = (TextView) findViewById(R.id.display_author);
art_publishtime = (TextView) findViewById(R.id.display_date);
art_content = (TextView) findViewById(R.id.display_content);
// display product data in TextView
int text_size = Integer.parseInt(sharedPrefs.getString("prefFontSize", "1"));
art_title.setText(product.getString(TAG_TITLE));
art_title.setTextSize(TypedValue.COMPLEX_UNIT_SP, text_size+20);
art_author.setText(product.getString(TAG_AUTHOR));
art_author.setTextSize(TypedValue.COMPLEX_UNIT_SP, text_size+14);
art_publishtime.setText(product.getString(TAG_PUBLISHTIME));
art_publishtime.setTextSize(TypedValue.COMPLEX_UNIT_SP, text_size+14);
art_content.setText(product.getString(TAG_CONTENT));
art_content.setTextSize(TypedValue.COMPLEX_UNIT_SP, text_size+18);
}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 file_url) {
// dismiss the dialog once got all details
pDialog.dismiss();
}
}
当前,您正在尝试访问Ui元素并从AsyncTask的
doInBackground
运行runOnUiThread
,这是不可能的
您需要在onPreExecute
中移动所有UI元素初始化,并在doInBackground执行完成时使用onPostExecute
更新UI元素。将您的AsyncTask
类创建为:
class GetProductDetails extends AsyncTask<String, String, String> {
// Declare UI elemnts here
@Override
protected void onPreExecute() {
super.onPreExecute();
// show ProgressDialog and initilize Ui elemnts
}
protected String doInBackground(String... args) {
// add your code here from makeing JSON request
//
return null;
}
protected void onPostExecute(String file_url) {
// Update UI when doInBackground exection completes
}
}
类GetProductDetails扩展异步任务{
//在此声明UI元素
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
//显示ProgressDialog并初始化Ui元素
}
受保护的字符串doInBackground(字符串…args){
//通过生成JSON请求在此处添加代码
//
返回null;
}
受保护的void onPostExecute(字符串文件\u url){
//doInBackground执行完成时更新UI
}
}
有关如何在应用程序中使用AsyncTask的更多信息,请参阅:
我将错误日志张贴在主帖子上。它太长了,无法放在评论中。你应该用ρцσѕρєK的答案。您无法从后台线程更新UI。因此,在受保护的字符串doInBackground(String…args){}中,我删除了runOnUiThread(new Runnable()?哇,对不起。非常感谢您的帮助!
class GetProductDetails extends AsyncTask<String, String, String> {
// Declare UI elemnts here
@Override
protected void onPreExecute() {
super.onPreExecute();
// show ProgressDialog and initilize Ui elemnts
}
protected String doInBackground(String... args) {
// add your code here from makeing JSON request
//
return null;
}
protected void onPostExecute(String file_url) {
// Update UI when doInBackground exection completes
}
}