为什么我的 AsyncTask 在主线程上进行网络操作?
Why my AsyncTask is doing network operation on Main Thread?
我在片段中有一个 AsyncTask,代码没有错误,但即使是 AsyncTask 也在主线程上执行网络操作。
AsyncTask 确实执行了,但我不明白为什么它在主线程上执行 newtwork 操作。
这是我的代码:
public class Tappal extends ActionBarActivity implements ActionBar.TabListener {
//codes of Tappal class
public static class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = null;
int position = getArguments().getInt(ARG_SECTION_NUMBER);
if (position == 1) {Toast.LENGTH_SHORT).show();
new MyTask().execute("key", "id");
} else if (position == 2)
rootView = inflater.inflate(R.layout.fragment_history, container, false);
else
rootView = inflater.inflate(R.layout.fragment_update, container, false);
return rootView;
}
private class MyTask extends AsyncTask<String, String, HttpResponse> {
HttpResponse httpresponse = null;
HttpEntity httpentity = null;
String response = null;
protected HttpResponse doInBackground(String... params) {
publishProgress("Getting Tappal Details...");
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://***/***/***/****/");
try {
httpresponse = httpclient.execute(httppost);
} catch (Exception e) {
publishProgress(e.toString());
}
return httpresponse;
}
protected void onProgressUpdate(String... i) {
Toast.makeText(getActivity(), i[0], Toast.LENGTH_SHORT).show();
}
protected void onPostExecute(HttpResponse r) {
try {
httpentity = r.getEntity();
response = EntityUtils.toString(httpentity);
JSONObject t = new JSONObject(response);
Toast.makeText(getActivity(), "JSON GOT", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(getActivity(), e.toString(), Toast.LENGTH_SHORT).show();
}
}
}
}
}
因为您在 onPostExecute() 中使用 HttpResponse。 onPostExecute 在主线程中工作。
试试这个:
private class MyTask extends AsyncTask<String, String, JSONObject> {
HttpResponse httpresponse = null;
HttpEntity httpentity = null;
String response = null;
protected JSONObject doInBackground(String... params) {
publishProgress("Getting Tappal Details...");
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://***/***/***/****/");
JSONObject t = null;
try {
httpresponse = httpclient.execute(httppost);
httpentity = r.getEntity();
response = EntityUtils.toString(httpentity);
t = new JSONObject(response);
} catch (Exception e) {
publishProgress(e.toString());
}
return t;
}
protected void onProgressUpdate(String... i) {
Toast.makeText(getActivity(), i[0], Toast.LENGTH_SHORT).show();
}
protected void onPostExecute(JSONObject r) {
if(r != null){
Toast.makeText(getActivity(), "JSON GOT", Toast.LENGTH_SHORT).show();
//Do work with response
}
}
}
检查official doc。
onPostExecute(Result)
, invoked on the UI thread after the background computation finishes. The result of the background computation is passed to this step as a parameter.
在您的情况下,您正在 onPostExecute(Result)
方法中处理 httpresponse。您必须在 doInBackground
方法中移动这部分代码。
您可以尝试调试您的代码。
如果您尝试在 doInBackground 中使用断点,您将看到一个单独的线程。
在 onPostExecute 代码上执行相同操作,您将看到 MainThread。
我在片段中有一个 AsyncTask,代码没有错误,但即使是 AsyncTask 也在主线程上执行网络操作。 AsyncTask 确实执行了,但我不明白为什么它在主线程上执行 newtwork 操作。 这是我的代码:
public class Tappal extends ActionBarActivity implements ActionBar.TabListener {
//codes of Tappal class
public static class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = null;
int position = getArguments().getInt(ARG_SECTION_NUMBER);
if (position == 1) {Toast.LENGTH_SHORT).show();
new MyTask().execute("key", "id");
} else if (position == 2)
rootView = inflater.inflate(R.layout.fragment_history, container, false);
else
rootView = inflater.inflate(R.layout.fragment_update, container, false);
return rootView;
}
private class MyTask extends AsyncTask<String, String, HttpResponse> {
HttpResponse httpresponse = null;
HttpEntity httpentity = null;
String response = null;
protected HttpResponse doInBackground(String... params) {
publishProgress("Getting Tappal Details...");
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://***/***/***/****/");
try {
httpresponse = httpclient.execute(httppost);
} catch (Exception e) {
publishProgress(e.toString());
}
return httpresponse;
}
protected void onProgressUpdate(String... i) {
Toast.makeText(getActivity(), i[0], Toast.LENGTH_SHORT).show();
}
protected void onPostExecute(HttpResponse r) {
try {
httpentity = r.getEntity();
response = EntityUtils.toString(httpentity);
JSONObject t = new JSONObject(response);
Toast.makeText(getActivity(), "JSON GOT", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(getActivity(), e.toString(), Toast.LENGTH_SHORT).show();
}
}
}
}
}
因为您在 onPostExecute() 中使用 HttpResponse。 onPostExecute 在主线程中工作。 试试这个:
private class MyTask extends AsyncTask<String, String, JSONObject> {
HttpResponse httpresponse = null;
HttpEntity httpentity = null;
String response = null;
protected JSONObject doInBackground(String... params) {
publishProgress("Getting Tappal Details...");
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://***/***/***/****/");
JSONObject t = null;
try {
httpresponse = httpclient.execute(httppost);
httpentity = r.getEntity();
response = EntityUtils.toString(httpentity);
t = new JSONObject(response);
} catch (Exception e) {
publishProgress(e.toString());
}
return t;
}
protected void onProgressUpdate(String... i) {
Toast.makeText(getActivity(), i[0], Toast.LENGTH_SHORT).show();
}
protected void onPostExecute(JSONObject r) {
if(r != null){
Toast.makeText(getActivity(), "JSON GOT", Toast.LENGTH_SHORT).show();
//Do work with response
}
}
}
检查official doc。
onPostExecute(Result)
, invoked on the UI thread after the background computation finishes. The result of the background computation is passed to this step as a parameter.
在您的情况下,您正在 onPostExecute(Result)
方法中处理 httpresponse。您必须在 doInBackground
方法中移动这部分代码。
您可以尝试调试您的代码。
如果您尝试在 doInBackground 中使用断点,您将看到一个单独的线程。
在 onPostExecute 代码上执行相同操作,您将看到 MainThread。