Java 异步任务步骤 公共类HttpPostTask扩展了AsyncTask{ TextView txtStatus=(TextView)findViewById(R.id.txtStatus); @凌驾 受保护的Void doInBackground(Void…参数){ EditText editText1=(EditText)findViewById(R.id.editText1); 试一试{ HttpClient HttpClient=新的DefaultHttpClient(); HttpPost HttpPost=新的HttpPost(“http://83.254.xx.xx/android/service.php"); List nameValuePairs=新的ArrayList(2); 添加(新的BasicNameValuePair(“操作”、“保存数据”); 添加(新的BasicNameValuePair(“数据”,editText1.getText().toString()); setEntity(新的UrlEncodedFormEntity(nameValuePairs)); HttpResponse response=httpclient.execute(httppost); }捕获(例外e){ e、 printStackTrace(); } 返回null; } 受保护的void onPreExecute(){ Log.i(“调试”、“onPreExecute”); } 受保护的void onProgressUpdate(整数…进度){ Log.i(“调试”、“onProgressUpdate”+progress[0]); } 受保护的void onPostExecute(){ Log.i(“调试”、“onPostExecute”); } }
我在日志中看到的只是Java 异步任务步骤 公共类HttpPostTask扩展了AsyncTask{ TextView txtStatus=(TextView)findViewById(R.id.txtStatus); @凌驾 受保护的Void doInBackground(Void…参数){ EditText editText1=(EditText)findViewById(R.id.editText1); 试一试{ HttpClient HttpClient=新的DefaultHttpClient(); HttpPost HttpPost=新的HttpPost(“http://83.254.xx.xx/android/service.php"); List nameValuePairs=新的ArrayList(2); 添加(新的BasicNameValuePair(“操作”、“保存数据”); 添加(新的BasicNameValuePair(“数据”,editText1.getText().toString()); setEntity(新的UrlEncodedFormEntity(nameValuePairs)); HttpResponse response=httpclient.execute(httppost); }捕获(例外e){ e、 printStackTrace(); } 返回null; } 受保护的void onPreExecute(){ Log.i(“调试”、“onPreExecute”); } 受保护的void onProgressUpdate(整数…进度){ Log.i(“调试”、“onProgressUpdate”+progress[0]); } 受保护的void onPostExecute(){ Log.i(“调试”、“onPostExecute”); } },java,android,android-asynctask,Java,Android,Android Asynctask,我在日志中看到的只是onPreExecute。doInBackground中的代码工作正常,没有例外。我想用当前状态更新textview,但为什么不调用所有步骤 谢谢编辑:看起来问题被编辑了……我想这是因为您为AsyncTask方法声明了类型。你有 public class HttpPostTask extends AsyncTask<Void, Integer, Void> { TextView txtStatus = (TextView)findViewById(R.i
onPreExecute
。doInBackground
中的代码工作正常,没有例外。我想用当前状态更新textview,但为什么不调用所有步骤
谢谢编辑:看起来问题被编辑了……我想这是因为您为
AsyncTask
方法声明了类型。你有
public class HttpPostTask extends AsyncTask<Void, Integer, Void> {
TextView txtStatus = (TextView)findViewById(R.id.txtStatus);
@Override
protected Void doInBackground(Void... params) {
EditText editText1 = (EditText)findViewById(R.id.editText1);
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://83.254.xx.xx/android/service.php");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("action", "savedata"));
nameValuePairs.add(new BasicNameValuePair("data", editText1.getText().toString()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
protected void onPreExecute(){
Log.i("debug", "onPreExecute");
}
protected void onProgressUpdate(Integer... progress) {
Log.i("debug", "onProgressUpdate, " + progress[0]);
}
protected void onPostExecute() {
Log.i("debug", "onPostExecute");
}
}
原始答复:
AsyncTask
在与主UI线程不同的线程上运行,主UI线程控制UI(以及文本视图)。您需要在主UI线程上更新TextView。一种方法是将消息发送回主线程,并实现一个消息处理程序,然后更新TextView。两个问题:
onpostExecute
采用与doInBackground
的返回值类型相同的参数,因此您拥有的版本不是相同的方法签名(您实际上没有重写该方法),因此不会被调用。它应该是onPostExecute(无效结果)
onProgressUpdate
仅在从后台方法调用publishProgress
时才会被调用。如果不调用publish,则不会触发任何更新@Override
属性的习惯,编译器将发现像#1这样的问题
HTH在TextView上设置文本可以在这个类中正常工作。你是说这是一种坏习惯,还是说它根本不起作用?它似乎对我有用。“看起来问题被编辑了…”-唯一被编辑的是标题:)如果我使用
AsyncTask
而不是AsyncTask
,我就能够使用整数并将其返回到onPostExecute
。如果我没有错过什么,但它似乎在起作用。嗯……在这里也难住了!对于onProgressUpdate(),可能需要调用publishProgress(),如下所示:
AsyncTask<Void, Integer, Void>
AsyncTask<Void, Void, Void>