2017-06-09 83 views
-1

我有我的應用程序從鏈接獲取JSON數據,但我需要從JSON獲取狀態。JSON到textView?

  • Status> Code and Msg。

這裏是我的工作:

截圖:

enter image description here

我需要把它輸出>

  1. 代碼:成功(或失敗,無論json說)
  2. Msg:許可更新..(或任何J SON吐出)

任何幫助會很好,或指示我的方向,謝謝。

我的一些代碼:

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

     } 

回答

0

1.首先創建響應字符串words一個JSONObject

JSONObject jsonObject = new JSONObject(words); 

2.從您迴應的JSONObject words獲取的JSONObject status

JSONObject jsonObjectStatus = jsonObject.getJSONObject("status"); 

3。最後,解析codestatus的JSONObject msg字符串如下:如下

String code = jsonObjectStatus.getString("code"); 
    String msg = jsonObjectStatus.getString("msg"); 

更新onPostExecute()

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

     try { 
      JSONObject jsonObject = new JSONObject(words); 
      JSONObject jsonObjectStatus = jsonObject.getJSONObject("status"); 

      String code = jsonObjectStatus.getString("code"); 
      String msg = jsonObjectStatus.getString("msg"); 

      texx.setText("Code: " + code + "\nMessage: " + msg); 

     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
    } 
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(); 
    } 


    } 
+0

評論不適合廣泛的討論;這個對話已經[轉移到聊天](http://chat.stackoverflow.com/rooms/146440/discussion-on-answer-by-nilesh-rathod-json-to-textview)。 –

0

結帳真棒庫Gson 你只需要爲響應創建對象,你會得到它就像

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

response contains al你的需求。

OR

您可以json parser tutorials去。

0

塊引用

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

塊引用