本文介绍了基于JSON数据..如何在doInBackground上举报服务器状态以形成服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试显示有关服务器状态的吐司

I am trying to Show Toast on Server Status

这些是我在服务器中JSONdata的类型

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"
         }
    }

所以现在我要向用户举报数据,因为我的状态为204401

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

我尝试过

 @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

我想从服务器获取状态信息.有人可以建议我..我想在状态不等于200时进行祝酒信息

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

我在服务中执行的所有操作都不在活动中

推荐答案

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

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

这篇关于基于JSON数据..如何在doInBackground上举报服务器状态以形成服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 23:17