Android 为什么我的AsyncTask在主线程上执行网络操作?
我在片段中有一个AsyncTask,代码中没有错误,但即使AsyncTask也在主线程上执行网络操作。 AsyncTask确实执行,但我不明白它为什么在主线程上执行NewWork操作。 这是我的密码:Android 为什么我的AsyncTask在主线程上执行网络操作?,android,android-asynctask,fragment,Android,Android Asynctask,Fragment,我在片段中有一个AsyncTask,代码中没有错误,但即使AsyncTask也在主线程上执行网络操作。 AsyncTask确实执行,但我不明白它为什么在主线程上执行NewWork操作。 这是我的密码: public class Tappal extends ActionBarActivity implements ActionBar.TabListener { //codes of Tappal class public static class PlaceholderFragment e
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();
}
}
}
}
公共类Tappal扩展ActionBarActivity实现ActionBar.TabListener{
//Tappal类代码
公共静态类占位符片段扩展了片段{
私有静态最终字符串ARG\u SECTION\u NUMBER=“SECTION\u NUMBER”;
公共静态占位符片段newInstance(int sectionNumber){
占位符片段=新占位符片段();
Bundle args=新Bundle();
参数Putin(参数段号,段号);
fragment.setArguments(args);
返回片段;
}
公共占位符片段(){
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图rootView=null;
int position=getArguments().getInt(ARG\u节号);
如果(position==1){Toast.LENGTH_SHORT.show();
new MyTask().execute(“key”,“id”);
}否则如果(位置==2)
rootView=充气机。充气(R.layout.fragment\u历史,容器,false);
其他的
rootView=充气机。充气(R.layout.fragment\u更新,容器,false);
返回rootView;
}
私有类MyTask扩展了AsyncTask{
HttpResponse HttpResponse=null;
HttpEntity HttpEntity=null;
字符串响应=null;
受保护的HttpResponse doInBackground(字符串…参数){
出版进度(“获取Tappal详细信息…”);
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://***/***/***/**/”);
试一试{
httpresponse=httpclient.execute(httppost);
}捕获(例外e){
出版进度(如toString());
}
返回httpresponse;
}
受保护的void onProgressUpdate(字符串…i){
Toast.makeText(getActivity(),i[0],Toast.LENGTH_SHORT.show();
}
受保护的void onPostExecute(HttpResponse r){
试一试{
httpentity=r.getEntity();
response=EntityUtils.toString(httpentity);
JSONObject t=新的JSONObject(响应);
Toast.makeText(getActivity(),“JSON GOT”,Toast.LENGTH_SHORT.show();
}捕获(例外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
}
}
}
私有类MyTask扩展了AsyncTask{
HttpResponse HttpResponse=null;
HttpEntity HttpEntity=null;
字符串响应=null;
受保护的JSONObject doInBackground(字符串…参数){
出版进度(“获取Tappal详细信息…”);
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://***/***/***/**/”);
JSONObject t=null;
试一试{
httpresponse=httpclient.execute(httppost);
httpentity=r.getEntity();
response=EntityUtils.toString(httpentity);
t=新的JSONObject(响应);
}捕获(例外e){
出版进度(如toString());
}
返回t;
}
受保护的void onProgressUpdate(字符串…i){
Toast.makeText(getActivity(),i[0],Toast.LENGTH_SHORT.show();
}
受保护的void onPostExecute(JSONObject r){
如果(r!=null){
Toast.makeText(getActivity(),“JSON GOT”,Toast.LENGTH_SHORT.show();
//与回应一起工作
}
}
}
请检查
onPostExecute(Result)
,后台计算完成后在UI线程上调用。背景计算的结果作为参数传递到此步骤
在本例中,您正在用onPostExecute(Result)
方法处理httpresponse。您必须在doInBackground
方法中移动这部分代码
您可以尝试调试您的代码。如果您尝试在doInBackground中使用断点,您将看到一个单独的线程 在onPostExecute代码上执行同样的操作,您将看到主线程
你是如何在主线程上找到它的?@raghundandand通过烘烤e.toString()。