0

After completing the task ImageUploadTask(), the method itself will return sResponse which supposedly would trigger the onPostExecute(). However, I am unable to get onPostExecute() to work.

The code for the Intent is:

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

The ImageUploadTask() is:

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

The code for the onPostExecute() is:

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

Thanks in advance =D

Benflop
  • 13
  • 2
  • getting any error plz paste logcat here – Biraj Zalavadia Sep 02 '13 at 12:23
  • Benflop pls post the stack trace. you are probably getting an exception – Raghunandan Sep 02 '13 at 12:44
  • @Raghunandan but I am not receiving any exception...ImageUploadTask() was completed without exception but it still did not execute onPostExecute..i followed your advise..I typed in the following code: catch (Exception e) { e.printStackTrace(); return null; } – Benflop Sep 02 '13 at 12:52
  • @Benflop if `doInbackground` is executed after its execution onPostExecute is invoked according to the docs. Quoting from docs `onPostExecute(Result)`, **invoked on the UI thread after the background computation finishes**. The result of the background computation is passed to this step as a parameter. SO you might me getting exception – Raghunandan Sep 02 '13 at 12:56
  • @Raghunandan my onPostExecute method is below doInBackground but both are in the same ImageUploadTask class but not in the UI thread...does this mean doInBackground is executed before onPostExecute is invoked? – Benflop Sep 02 '13 at 13:16
  • @Benflop did you even read the docs. For more info check the docs under the topic The 4 steps. http://developer.android.com/reference/android/os/AsyncTask.html – Raghunandan Sep 02 '13 at 13:17
  • @Raghunandan i just read the docs. erm doInBackground is executed first and once doInBackground is completed it return sResponse, which is passed as a parameter in onPostExecute. i do not have any code running on onProgressUpdate and onPreExecute though... – Benflop Sep 02 '13 at 13:49
  • @Benflop then `doInbackgound` is not executed. may be you get exception – Raghunandan Sep 02 '13 at 13:52

4 Answers4

2

Change it to this:

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

Add the ...

Edit: also as @Raghunandan points out, you cannot call Toast from your doInBackground, it's a mere background thread and does not support UI commands. For that, you must use publishProgress() and onProgressUpdate().

KickAss
  • 4,210
  • 8
  • 33
  • 41
  • why this `(String... sResponse)` ? when op has `String sResponse = reader.readLine(); return sResponse` – Raghunandan Sep 02 '13 at 12:24
  • I'm not sure the role "..." plays. Usually for me, that's what fixes it :) I'm looking for an explanation. If I find it, I will add it to my answer :) – KickAss Sep 02 '13 at 12:26
  • this is not the answer to the question posted. its varargs http://stackoverflow.com/questions/766559/when-do-you-use-varargs-in-java – Raghunandan Sep 02 '13 at 12:27
0

Try this..

@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);
    }
}
}}
Hariharan
  • 24,741
  • 6
  • 50
  • 54
0

Your might be getting exception.

You have this in doInbackground.

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

doInbackground is invoked on a background thread. You cannot display toast/ update ui from doInbackground. So return response and display toast in onPostExecute

You need to change this

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

To

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

For more info check the docs under the topic The 4 steps.

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

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
0

Probably your doInBackGround method is throwing an exception. Also you can remove super. super.onPostExecute() as it is redundant.

khubaib
  • 535
  • 4
  • 12