0

完成 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

4 回答 4

2

将其更改为:

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

添加...

编辑: 也正如所@Raghunandan指出的,你不能Toast从你的 调用doInBackground,它只是一个后台线程,不支持 UI 命令。为此,您必须使用publishProgress()and onProgressUpdate()

于 2013-09-02T12:22:46.703 回答
0

可能您的 doInBackGround 方法正在引发异常。您也可以删除超级。super.onPostExecute() 因为它是多余的。

于 2013-09-02T13:24:19.807 回答
0

您可能会遇到异常。

你有这个在doInbackground

 Toast.makeText(getApplicationContext(),
            getString(R.string.exception_message),
            Toast.LENGTH_LONG).show();

doInbackground在后台线程上调用。您无法显示 toast/更新 ui 从doInbackground. 所以返回响应并显示吐司onPostExecute

你需要改变这个

     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();
      }

有关更多信息,请查看主题The 4 steps下的文档。

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

于 2013-09-02T12:31:28.220 回答
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);
    }
}
}}
于 2013-09-02T12:27:50.797 回答