public class myAsyncTask extends AsyncTask<String,Integer,String> {
//取消异步任务时会调用该方法
@Override
protected void onCancelled() {
super.onCancelled();
}
//准备开始执行异步任务
@Override
protected void onPreExecute() {
super.onPreExecute();
}
String ip,parameter;
Handler handler;
//doInBackground()方法用于在执行异步任务,不可以更改主线程中UI
@Override
protected String doInBackground(final String... params) {
String s = null;
return s;
}
public myAsyncTask(String ip, String parameter, Handler handler){
this.ip =ip;
this.parameter = parameter;
this.handler =handler;
}
//onPostExecute()方法用于异步任务执行完成后,在主线程中执行的操作
@Override protected void onPostExecute(String s) {
super.onPostExecute(s);
try {
JSONObject jsonObject = new JSONObject(s);
Message message = new Message();
message.obj =jsonObject;
handler.sendMessage(message);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
main
myAsyncTask myAsyncTask = new myAsyncTask("s","s", new Handler(){ @Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
}
});
//开启异步myAsyncTask.execute();
//取消一个正在执行的任务,onCancelled()方法将会被调用myAsyncTask.cancel(true);
网友评论