onPostExecute不会执行

时间:2013-09-02 12:21:24

标签: android android-asynctask

完成ImageUploadTask()任务后,方法本身将返回sResponse,据说会触发onPostExecute()。但是,我无法使用onPostExecute()来工作。

意图的代码是:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

 upload.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            if (bitmap == null) {
                Toast.makeText(getApplicationContext(),
                        "Please select image", Toast.LENGTH_SHORT).show();
            } else if (subject.getText() == null) {
                Toast.makeText(getApplicationContext(),
                        "Please enter subject title", Toast.LENGTH_SHORT).show();
            } else if (msg == null) {
                Toast.makeText(getApplicationContext(),
                        "Please enter message", Toast.LENGTH_SHORT).show();
            } else {                
                dialog = ProgressDialog.show(MainActivity.this, "Uploading",
                        "Please wait...", true);
                new ImageUploadTask().execute();
            }
        }
    });

ImageUploadTask()是:

class ImageUploadTask extends AsyncTask <Void, Void, String>{
@Override
protected String doInBackground(Void... params) {
    try {
        HttpClient httpClient = new DefaultHttpClient();
        HttpContext localContext = new BasicHttpContext();
        HttpPost httpPost = new HttpPost("http://203.117.178.181/test3/postdata2.php");

        MultipartEntity entity = new MultipartEntity(
                HttpMultipartMode.BROWSER_COMPATIBLE);

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bitmap.compress(CompressFormat.JPEG, 100, bos);
        byte[] data = bos.toByteArray();
        entity.addPart("userfile", new ByteArrayBody(data,
                "myImage.jpg"));
        entity.addPart("subject", new StringBody(subject.getText()
                .toString()));
        entity.addPart("message", new StringBody(msg.getText()
                .toString()));
        httpPost.setEntity(entity);
        HttpResponse response = httpClient.execute(httpPost,
                localContext);
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(
                        response.getEntity().getContent(), "UTF-8"));

        String sResponse = reader.readLine();
        return sResponse;

    } catch (Exception e) {
        if (dialog.isShowing())
            dialog.dismiss();
        Toast.makeText(getApplicationContext(),
                getString(R.string.exception_message),
                Toast.LENGTH_LONG).show();
        Log.e(e.getClass().getName(), e.getMessage(), e);
        return null;
    }
    // (null);
}  

onPostExecute()的代码是:

@Override
protected void onPostExecute(String sResponse) {
    super.onPostExecute(sResponse);
    try {
        if (dialog.isShowing())
            dialog.dismiss();

        if (sResponse != null) {
            JSONObject JResponse = new JSONObject(sResponse);
            int success = JResponse.getInt("SUCCESS");
            String message = JResponse.getString("MESSAGE");
            if (success == 0) {
                Toast.makeText(getApplicationContext(), message,
                        Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(getApplicationContext(),
                        "Photo uploaded successfully",
                        Toast.LENGTH_SHORT).show();
                subject.setText("");
                msg.setText("");
            }
        }
    } catch (Exception e) {
        Toast.makeText(getApplicationContext(),
                getString(R.string.exception_message),
                Toast.LENGTH_LONG).show();
        Log.e(e.getClass().getName(), e.getMessage(), e);
    }
}
}}

提前致谢= D

4 个答案:

答案 0 :(得分:1)

将其更改为:

@Override
protected void onPostExecute(String... sResponse) {

添加...

修改:同时@Raghunandan指出,您无法从Toast调用doInBackground,它只是一个后台线程,不支持UI命令。为此,您必须使用publishProgress()onProgressUpdate()

答案 1 :(得分:0)

试试这个..

@Override
protected void onPostExecute(String sResponse) {

        if (dialog.isShowing())
            dialog.dismiss();
     try {
        if (sResponse != null) {
            JSONObject JResponse = new JSONObject(sResponse);
            int success = JResponse.getInt("SUCCESS");
            String message = JResponse.getString("MESSAGE");
            if (success == 0) {
                Toast.makeText(getApplicationContext(), message,
                        Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(getApplicationContext(),
                        "Photo uploaded successfully",
                        Toast.LENGTH_SHORT).show();
                subject.setText("");
                msg.setText("");
            }
        }
    } catch (Exception e) {
        Toast.makeText(getApplicationContext(),
                getString(R.string.exception_message),
                Toast.LENGTH_LONG).show();
        Log.e(e.getClass().getName(), e.getMessage(), e);
    }
}
}}

答案 2 :(得分:0)

你可能会遇到异常。

你在doInbackground中有这个。

 Toast.makeText(getApplicationContext(),
            getString(R.string.exception_message),
            Toast.LENGTH_LONG).show();
在后台线程上调用

doInbackground。您无法从doInbackground显示toast / update ui。所以返回响应并在onPostExecute

中显示toast

您需要更改此

     catch (Exception e) {
    if (dialog.isShowing())
        dialog.dismiss();
    Toast.makeText(getApplicationContext(),
            getString(R.string.exception_message),
            Toast.LENGTH_LONG).show();
    Log.e(e.getClass().getName(), e.getMessage(), e);
    return null;
}

     catch (Exception e) {
          e.printStacktrace();
      }

有关详细信息,请查看主题 4个步骤下的文档。

http://developer.android.com/reference/android/os/AsyncTask.html

答案 3 :(得分:0)

可能你的doInBackGround方法抛出异常。你也可以删除超级。 super.onPostExecute()因为它是多余的。