基于JSON数据。如何在DoInBackground中的Toast服务器状态形成服务

时间:2018-06-15 06:52:50

标签: android android-asynctask toast

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

这些是我的服务器{/ 1}的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中设置状态

我想从服务器看Toast状态可以任何人建议我这种类型..我想在状态不等于200时Toast

所有这些我在服务中做的非活动

2 个答案:

答案 0 :(得分:3)

尝试这样做

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

答案 1 :(得分:1)

您可以使用Array ( [0] => Array ( [0] => 8 [1] => 2 [2] => 7 [3] => 3 [4] => 6 [5] => 5 ) [1] => Array ( [0] => 3 [1] => 8 [2] => 5 [3] => 7 [4] => 1 [5] => 2 ) [2] => Array ( [0] => 1 [1] => 3 [2] => 4 [3] => 5 [4] => 2 [5] => 7 ) [3] => Array ( [0] => 7 [1] => 5 [2] => 9 [3] => 1 [4] => 4 [5] => 3 ) [4] => Array ( [0] => 4 [1] => 5 [2] => 6 [3] => 7 [4] => 9 [5] => 1 ) [5] => Array ( [0] => 6 [1] => 8 [2] => 1 [3] => 9 [4] => 7 [5] => 4 ) ) 方法中的Handler轻松实现此目的:

doInBackground