我是使用AsyncTask的新手。它冻结了我的程序,我不知道为什么。我正在寻找一种方法来返回我放入arrayList中的数据库结果。我用断点和我的doInBackground(String ... args)抓取我想从我的本地数据库中获得的信息。之后,我的程序冻结了。我不知道为什么。我已经在堆栈和谷歌上查找了解决方案,发现很多人都在使用.get(),他们不应该这样做。除非有什么我想念我不是。这里是我的代码,任何想法将不胜感激。AsyncTask冻结我的用户界面,我没有使用.get()
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
productsList = new ArrayList<HashMap<String, String>>();
task = new LoadAllProducts();
task.execute("");
}
class LoadAllProducts extends AsyncTask<String, String, String> {
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = null;
try {
json = jParser.makeHttpRequest (url_all_product, "GET",
params);
} catch (ClientProtocolException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// Check your log cat for JSON reponse
Log.d("All Products: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
products = json.getJSONArray(TAG_VISITORS);
// looping through All Products
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_PID);
String name = c.getString(TAG_NAME);
String company = c.getString(TAG_COMPANY);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_PID, id);
map.put(TAG_NAME, name);
map.put(TAG_COMPANY, company);
// adding HashList to ArrayList
productsList.add(map);
}
} else {
// no products found
// Launch Add New product Activity
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
}
你确定它结冰了,不仅花了很长时间来处理?那些是不同的东西。我建议在你的'onCreate()'方法的每一行后面加上'Log.d()'行,看看瓶颈在哪里。 – nKn
我的程序还没有做任何事情,它已经10分钟 – Aaron
也许你的'AsyncTask'没有返回你期望的答案,但这并不意味着你的'AsyncTask'阻止了执行(10分钟是一个巨大的时间挂起,它会被吊死)。我仍然推荐'Log.d()'技术,看看会发生什么。 – nKn