基于 JSON 数据..如何在 doInBackground 表单服务中吐司服务器状态

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

我正在尝试在服务器状态上显示 Toast

这些是我来自服务器

JSONdata类型
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"
         }
    }

所以现在我想在状态为 204401

时向用户吐司数据

我试过

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

我关注了 this 但我无法在 Toast 中设置状态

我想从服务器吐司状态 任何人都可以在这种情况下给我建议吗..我想在状态不等于 200 时吐司

我所做的一切都是在服务中而不是在 Activity

尝试这样做

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

您可以在 doInBackground 方法中使用 Handler 轻松实现此目的:

        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getApplicationContext(), "Your text here", Toast.LENGTH_SHORT).show();
                //your toast here
            }
        });