Based on JSON data..How to Toast server Status at doInBackground form a Service

北城余情 提交于 2019-12-01 14:21:43

问题


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


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



回答2:


Try it doing this way

runOnUiThread(new Runnable() {
            @Override
            public void run() {
                //Your Toast Here
            }
        });


来源:https://stackoverflow.com/questions/50870504/based-on-json-data-how-to-toast-server-status-at-doinbackground-form-a-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!