建议有关的AsyncTask [英] Suggestion about AsyncTask
问题描述
在这个话题:
关于任务onPostExecute
我问onPostExecute解雇的过程中,朋友@ inner_class7给我解决错误的方式,但朋友@Doug史蒂文森说,使用这种方式,code会阻塞uithread,我测试了这是真的,我uithraed被封堵。所以,他说,我需要使用onPostExecute得到的结果。我了解并创建了一个code,我想了解的建议。
我改变了code和做到这一点:
I asked about onPostExecute to dismiss a process, the friend @inner_class7 give me the way to resolve the error, but the friend @Doug Stevenson said that using this way the code will blocking the uithread, I tested and this is true, my uithraed was blocked. So He said that I need to use onPostExecute to get the result. I read about and created a code and I would like suggestion about. I changed the code and do it:
protected ArrayList<String> doInBackground(String... params) {
try {
final String POST_PARAMS = params[1];
URL obj = new URL(params[0]);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", "Mozilla/5.0");
// For POST only - START
con.setDoOutput(true);
OutputStream os = con.getOutputStream();
os.write(POST_PARAMS.getBytes());
os.flush();
os.close();
// For POST only - END
int responseCode = con.getResponseCode();
System.out.println("POST Response Code :: " + responseCode);
if (responseCode == HttpURLConnection.HTTP_OK) { //success
BufferedReader in = new BufferedReader(new InputStreamReader(
con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// print result
System.out.println(response.toString());
JSONArray myListsAll= new JSONArray(response.toString());
for(int i=0;i<myListsAll.length();i++){
JSONObject jsonObject = myListsAll.getJSONObject(i);
this.stringArray.add(jsonObject.toString());
}
} else {
System.out.println("POST request not worked");
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return this.stringArray;
}
@Override
protected void onPostExecute(ArrayList<String> result)
{
this.obj.setFeedsData(result);
if(setProgress){ progress.dismiss(); }
}
因此,在我mainactivity拨打:
So call in my mainactivity:
@Override
protected void onCreate(Bundle savedInstanceState) {
String[] itensUrl = {links.feedsList(),"iduser=2&offset=0"};
new JsonRequest(this,this,true).execute(itensUrl);
}
public void setFeedsData(ArrayList<String> obj){
createListView(obj);
}
那么,你觉得呢?这样一来,一个用途是一个很好的方式?
So what Do you think about? This way that a use is a good way?
推荐答案
你有什么要工作得很好。你是否正确执行和解析出UI线程的请求的结果。不过,也有一对夫妇的小事情可以做,以使其更清晰。
What you have should be working just fine. You are correctly executing and parsing the results of the request off the UI thread. However, there are a couple minor things you can do to make it clearer.
字符串数组
看起来像在的AsyncTask
字段。这是没有必要你所定义的的AsyncTask
,因为你已经传递的方式字符串数组
直接进入 onPostExecute()
方法。而应该声明字符串数组
直接在 doInBackground()
方法。
stringArray
looks like a field on the AsyncTask
. This is not necessary the way you have the AsyncTask
defined since you are already passing the stringArray
directly into the onPostExecute()
method. You should instead declare stringArray
directly in the doInBackground()
method.
我注意到另一件事是它看起来像你可能会保持到你的活动
在基于的AsyncTask
参考您在 onPostExecute在做什么()
。您正在呼叫 this.obj.setFeedsData(结果);
。如果你的的AsyncTask
是一个内部类的活动
使用它,你可以调用 setFeedsData(结果);
,而不是直接
Other thing I noticed is it looks like you might be keeping a reference to your Activity
in the AsyncTask
based on what you are doing in onPostExecute()
. You are calling this.obj.setFeedsData(result);
. If your AsyncTask
is an inner class of the Activity
using it you can call setFeedsData(result);
directly instead.
如果你的的AsyncTask
不是一个内部类通常最好通过一个接口传递的结果反馈给他们感兴趣的对象的情况下,你需要重复使用的AsyncTask
在别处。
If your AsyncTask
is not an inner class it is usually best to pass the results back to the object interested in them through an interface in case you need to reuse the AsyncTask
elsewhere.
这篇关于建议有关的AsyncTask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!