Android AsyncTask使用心得及错误处理

大家肯定都会经常使用AsyncTask这个类,特别是在网络处理中,先看改正后的代码:这是正常的代码:

class sendKeyTask extends AsyncTask<String, Void, Integer>{@Overrideprotected void onPostExecute(Integer resultCode) {// TODO Auto-generated method stubsuper.onPostExecute(resultCode);switch (resultCode) {case 6000:NotifyHelper.popNotifyInfo(InnerQuestionActivity.this, "用户信息异常", "");break;case 6001:NotifyHelper.popNotifyInfo(InnerQuestionActivity.this, "其他异常", "");break;case 6002:break;default:break;}// 隐藏输入法InputMethodManager imm = (InputMethodManager) getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);// 显示或者隐藏输入法imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);innerQuestionEdit.setText("");//从新刷新new getQuestionDetailTack().execute(1);}@Overrideprotected Integer doInBackground(String… data) {// TODO Auto-generated method stubint resultCode=4001;HttpClient client= new DefaultHttpClient();HttpPost post = new HttpPost("");StringBuilder builder = new StringBuilder();List<NameValuePair> paramsList=new ArrayList<NameValuePair>();paramsList.add(new BasicNameValuePair("access_token", data[0]));paramsList.add(new BasicNameValuePair("user_name", data[1]));paramsList.add(new BasicNameValuePair("key_detail", data[2]));paramsList.add(new BasicNameValuePair("question_id", data[3]));for(int i=0;i<jpegDataList.size();i++){paramsList.add(new BasicNameValuePair("img"+String.valueOf(i), jpegDataList.get(i)));}try {post.setEntity( new UrlEncodedFormEntity(paramsList,HTTP.UTF_8));} catch (UnsupportedEncodingException e1) {e1.printStackTrace();}try {HttpResponse response = client.execute(post);HttpEntity entity = response.getEntity();BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent()));for (String s = reader.readLine(); s != null; s = reader.readLine()) {builder.append(s);}JSONObject jsonObject = new JSONObject(builder.toString());String stateCodeStr = jsonObject.getString("state_code");resultCode=Integer.parseInt(stateCodeStr);}catch (ClientProtocolException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (JSONException e) {// TODO Auto-generated catch blocke.printStackTrace();//处理请求失败}finally{}return resultCode;}}可能有人会说,我让doInBackground返回一个参数,,再在onPostExecute里面处理不是多次一举吗?但是,当你真的将两部分合成后,会发现,竟然报错了!报错内容大体为UI内容只能在主线程更改;这是为什么呢!

NotifyHelper.popNotifyInfo(InnerQuestionActivity.this, "其他异常", "");是对对话提示框的一个弹出方法封装,这是对UI界面的操作,问题应该就出在这儿了!

我们翻开google的说明看下:

Added in API level 3

Override this method to perform a computation on a background thread. The specified parameters are the parameters passed toexecute(Params…) by the caller of this task. This method can callpublishProgress(Progress…) to publish updates on the UI thread.

Parameters

paramsThe parameters of the task.

ReturnsA result, defined by the subclass of this task.

Added in API level 3

Runs on the UI thread after doInBackground(Params…). The specified result is the value returned bydoInBackground(Params…).

This method won’t be invoked if the task was cancelled.

Parameters

resultThe result of the operation computed by doInBackground(Params…).

See Also

Added in API level 3

Runs on the UI thread before doInBackground(Params…).

我们可以看出,这几个重载方法只有doInBackground是在后台线程运行的,而后台是不能执行更新线程的操作的!

我们必须要在doInbackground中,返回耗时操作的处理结果,再从onPostExecute中根据doInBackground返回的参数进行UI组件的操作!

doInBackground

读书破万卷,下笔如有神。

Android AsyncTask使用心得及错误处理

相关文章:

你感兴趣的文章:

标签云: