-3

None of the ToastMessages in this code are shown.

 private final class FetchUrlLINKTOINCREASECOIN extends AsyncTask<Void, Void, String> {
    @Override
    protected String doInBackground(Void... params) {
        try {
            Toast.makeText(MainActivity.this, "THIS IS NOT SHOWN", Toast.LENGTH_SHORT).show();

            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("https://example.com/xx.php");
            try {
                // Add your data
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                nameValuePairs.add(new BasicNameValuePair("userid", f12139v0));
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                // Execute HTTP Post Request
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                String responseString = EntityUtils.toString(entity, "UTF-8");
                Pattern pattern = Pattern.compile(",\"postid\":\"(.*?)\",\"");
                Matcher matcher = pattern.matcher(responseString);
                if (matcher.find())
                {
                    String req_id = matcher.group(1);
                    Toast.makeText(MainActivity.this, req_id, Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(MainActivity.this, "n", Toast.LENGTH_SHORT).show();
                }
                Toast.makeText(MainActivity.this, responseString, Toast.LENGTH_SHORT).show();
                } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
            } catch (IOException e) {
                // TODO Auto-generated catch block
            }
        } catch (Exception e) {
        }
        return "executed";
    }
}

But the web request is fine. This is how i run this function:

Toast.makeText(MainActivity.this, "This is fine", Toast.LENGTH_SHORT).show();
                new FetchUrlLINKTOINCREASECOIN().execute();

As you can see i put a toastmessage before running the function and the app showed that.

  • 1
    doInBackground is not supposed to do anything in the ui If you want to debug use logs instead. Log also the catch blocks in case something go wrong. Besides that, AsynkTasks are deprecated. – jeprubio Apr 17 '20 at 12:28

2 Answers2

0

doInBackground() will run in the worker thread. You don't have control over the UI thread to execute toast message.

You need to display in the onPostExecute method only.

Otherwise use Logs.

Hope this will help you!

Magudesh
  • 419
  • 5
  • 13
0

Since this is another thread and not the UI thread, UI operations like showing a Toast are not possible:

You have two choices:

one

Either use the onPostExecute() and onProgressUpdate() to update UI.

two

Call your Toasts in doInBackground() like this:

runonUiThread(new Runnable(){

public void run(){

    Toast.makeText(MainActivity.this, "THIS IS NOT SHOWN", Toast.LENGTH_SHORT).show();

}

});
Hasan Bou Taam
  • 4,017
  • 2
  • 12
  • 22