JSON到textView?

时间:2017-06-09 07:26:50

标签: android android-studio

我的应用程序从链接获取JSON数据,但我需要从JSON获取状态。

  • 状态>代码和消息

以下是我的工作:

截图:

enter image description here

我需要输出>

  1. 代码:成功(或失败,无论json说什么)
  2. 消息:许可证已更新..(或者JSON吐出的任何内容)
  3. 任何帮助都会很好或指向我的方向,谢谢。

    我的一些代码:

    public class doit extends AsyncTask<Void,Void,Void>{
    String words;
    
            public doit() throws JSONException {
            }
    
            @Override
            protected Void doInBackground(Void... voids) {
    
                try {
                    WebView wv = (WebView) findViewById(R.id.webview);
                    EditText messages = (EditText) findViewById(R.id.messages);
                    Document doc = Jsoup.connect("https://api.deepbot.tv/web/apply/namerefresh.php?s=" + messages.getText().toString()).get();
    
                    words=doc.text();
    
                }catch(Exception e){e.printStackTrace();}
    
                return null;
            }
    
            @Override
            protected void onPostExecute(Void aVoid) {
                super.onPostExecute(aVoid);
                texx.setText(words);
    
            }
    

4 个答案:

答案 0 :(得分:0)

尝试以下操作,只需替换您的退货:

   public class doit extends AsyncTask<Void,Void,Void>{
   String words;

    public doit() throws JSONException {
    }

    @Override
    protected Void doInBackground(Void... voids) {

        try {
            WebView wv = (WebView) findViewById(R.id.webview);
            EditText messages = (EditText) findViewById(R.id.messages);
            Document doc = Jsoup.connect("https://api.deepbot.tv/web/apply/namerefresh.php?s=" + messages.getText().toString()).get();

            words=doc.text();

        }catch(Exception e){e.printStackTrace();}

        return words;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);

    try {
        JSONObject  jObj = new JSONObject(words);
        String code = jObj.getString("code");
        String name = jObj.getString("msg");
        texx.setText(code +"  "+name);
    } catch (JSONException e) {
        e.printStackTrace();
    }


    }

答案 1 :(得分:0)

Checkout awesome library Gson 你只需要为响应创建Object,你就会得到它

Response response = gson.fromJson("words", Response.class);

响应包含您的所有需求。

您可以使用json parser tutorials

答案 2 :(得分:0)

  

块引用

JSONObject jObj = arr.getJSONObject(words);
String date = jObj.getString("msg");
  

块引用

答案 3 :(得分:0)

1。首先创建一个带有响应字符串JSONObject的{​​{1}}。

words

2. 从您的回复JSONObject JSONObject jsonObject = new JSONObject(words); 获取JSONObject status

words

3。最后,从 JSONObject jsonObjectStatus = jsonObject.getJSONObject("status"); JSONObject解析codemsg字符串,如下所示:

status

更新 String code = jsonObjectStatus.getString("code"); String msg = jsonObjectStatus.getString("msg"); ,如下所示:

onPostExecute()