代码之家  ›  专栏  ›  技术社区  ›  Whats Going On

基于json数据..如何在doinbackground上吐司服务器状态以形成服务

  •  -1
  • Whats Going On  · 技术社区  · 6 年前

    我正试图显示Toast on服务器状态

    这些是我喜欢的类型 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"
             }
        }
    

    所以现在我想向用户吐司我的状态为 204 401

    我尝试过

     @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 但我无法在祝酒词中设置状态

    我想从服务器上吐司状态有人能推荐我这种..当状态不等于200时我想吐司

    我所做的一切都是为了服务而不是为了活动

    2 回复  |  直到 6 年前
        1
  •  1
  •   Anatolii    6 年前

    你可以很容易地做到这一点 Handler 在你的内心 doInBackground 方法:

            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
  •  3
  •   Vir Rajpurohit    6 年前

    试试这样做

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