-1

I am trying to Show Toast on Server Status

These are my Types of JSONdata from Server

1) {
    "status": "200",
    "response": {
    "SortAs": "SGML",
    "GlossTerm": "Standard Generalized Markup Language",
    "Acronym": "SGML",
    "Abbrev": "ISO 8879:1986",
     .
     ..
     .
   }
}

2) {
    "status": "204",
    "response": {
       "msg": "No Content"
      }
   }

3) {
    "status": "401",
    "response": {
        "msg": "Unauthorized User"
         }
    }

So Now Here I want to Toast Data to the User wen I got Status as 204 or 401 etc

I tried with

 @Override
 protected Void doInBackground(Void... arg0) {
  HttpServiceClass httpServiceClass = new HttpServiceClass(HttpJSonURL);

try {
    httpServiceClass.ExecutePostRequest();
    if (httpServiceClass.getResponseCode() == 200) {
        FinalJSonResult = httpServiceClass.getResponse();
        if (FinalJSonResult != null) {

            try {
                JSONObject JObject = new JSONObject(FinalJSonResult);
                String status = JObject.getString("status");
                Log.v("ReturnStatus -",status);
                if(status.equals("200")) {
                    JSONArray response =JObject.getJSONArray("response");

                    for (int i = 0; i < response.length(); i++) {
                        JSONObject res = response.getJSONObject(i);
                        String stock_id = res.getString("stock_id");
                        String upc_no = res.getString("upc_no");
                        String stock_name = res.getString("stock_name");
                         .
                         .
                    }
                }
                else if(status.equals("401")) {
                    //Toast.makeText(context, "Unauthorized User", Toast.LENGTH_LONG).show();
                    Log.v("401 Error","Unauthorized User");

                }
                else if(status.equals("204")) {
                    Toast.makeText(getApplicationContext(), getString(R.string.e204), Toast.LENGTH_LONG).show();
                    Log.v("204 Error","Data not Set to Request");
                }
                else if(status.equals("400")) {
                    //Toast.makeText(context, "Bad Request", Toast.LENGTH_LONG).show();
                    Log.v("400 Error","Bad Request");
                }

            }
            catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    else {
        Toast.makeText(context, httpServiceClass.getErrorMessage(), Toast.LENGTH_SHORT).show();
    }
}
catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

return null;
}

I followed this but I am unable to Set Status inside the Toast

I want to Toast Status from the server Can any one suggest me on this kind..I want to Toast when status is not equal to 200

All this I am Doing in a Service not in Activity

Whats Going On
  • 1,379
  • 6
  • 20
  • 49
  • 1
    I already Given that in my code its not duplicate I already said that I followed https://stackoverflow.com/questions/13790351/how-to-show-toast-in-asynctask-in-doinbackground – Whats Going On Jun 15 '18 at 06:58
  • Please Help me on this don't.. blindly downvote or Mark Duplicate.. I am asking Based on JSON data why you are not thinking on that way – Whats Going On Jun 15 '18 at 07:02

2 Answers2

3

Try it doing this way

runOnUiThread(new Runnable() {
            @Override
            public void run() {
                //Your Toast Here
            }
        });
Vir Rajpurohit
  • 1,869
  • 1
  • 10
  • 23
1

You can easily achieve this with Handler inside your doInBackground method:

        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getApplicationContext(), "Your text here", Toast.LENGTH_SHORT).show();
                //your toast here
            }
        });
Anatolii
  • 14,139
  • 4
  • 35
  • 65
  • @MLN You can run it from anywhere because it will deliver the Runnable to the main thread anyways. – Anatolii Jun 15 '18 at 07:31
  • Actually my Service Runs for every 1 min...Its working for one time only... But After that its closing entire app and service... I am getting this in log `D/DisplayManager: unregisterDisplayListener index = 1` and `I/System.out: [CDS]close[58946] close [socket][/0.0.0.0:58946]` – Whats Going On Jun 15 '18 at 07:32
  • What's working for one time only? This piece of code? – Anatolii Jun 15 '18 at 07:34
  • yes... I added your Code in `elseif`... I am getting toast but for second time total app is closing its not showing any error – Whats Going On Jun 15 '18 at 07:36
  • Strange, could you send a full stacktrace? – Anatolii Jun 15 '18 at 07:59
  • Actually I am Minimizing Task after 30 seconds.. Before adding this coded its minimized and worked I can run other Background Services.. But after adding this I am getting `System.out: [CDS]close[60352] close [socket][/0.0.0.0:60352]`.. Before this its minimized and worked fine.. – Whats Going On Jun 15 '18 at 08:02
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/173198/discussion-between-user1055395-and-mln). – Anatolii Jun 15 '18 at 08:04