2016-08-13 117 views
1

顯示所有的JSON值目前正在研究這個教程http://www.android-examples.com/android-json-parsing-retrieve-from-url-and-set-mysql-db-data/我如何在android系統

它運行完美,但現在我想顯示所有的JSON值的文本視圖。我是JSON的新手,只有一點android的經驗。

這是我的MainActivity.java。我修改了一下教程

public class MainActivity extends Activity { 

TextView textview; 
JSONObject json = null; 
String str = ""; 
HttpResponse response; 
Context context; 
ProgressBar progressbar; 
Button button; 
JSONArray jArray; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    progressbar = (ProgressBar)findViewById(R.id.progressBar1); 
    textview = (TextView)findViewById(R.id.textView1); 
    button = (Button)findViewById(R.id.button1); 

    progressbar.setVisibility(View.GONE); 

    button.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

      progressbar.setVisibility(View.VISIBLE); 

      new GetTextViewData(context).execute(); 

     } 
    }); 
} 

public static Map<String,String> parse(JSONObject json , Map<String,String> out) throws JSONException{ 
    Iterator<String> keys = json.keys(); 
    while(keys.hasNext()){ 
     String key = keys.next(); 
     String val = null; 
     try{ 
      JSONObject value = json.getJSONObject(key); 
      parse(value,out); 
     }catch(Exception e){ 
      val = json.getString(key); 
     } 

     if(val != null){ 
      out.put(key,val); 
     } 
    } 
    return out; 
} 

private class GetTextViewData extends AsyncTask<Void, Void, Void> 
{ 
    public Context context; 

    public GetTextViewData(Context context) 
    { 
     this.context = context; 
    } 

    @Override 
    protected void onPreExecute() 
    { 
     super.onPreExecute(); 
    } 

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

     HttpClient myClient = new DefaultHttpClient(); 
     HttpPost myConnection = new HttpPost("http://192.168.1.9:80/test-androidex/send-data.php"); 

     try { 
      response = myClient.execute(myConnection); 
      str = EntityUtils.toString(response.getEntity(), "UTF-8"); 

     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 


     try{ 
      JSONArray jArray = new JSONArray(str); 
      json = jArray.getJSONObject(0); 

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

     catch (Exception e) 
     { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     return null; 
    } 
    protected void onPostExecute(Void result) 
    { 

    try { 
      textview.setText(json.getString("name")); 

     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     progressbar.setVisibility(View.GONE); 

    } 
} 

這是我的JSON。這與教程有很大的不同

[{「id」:「1」,「name」:「white」,「status」:「0」},{「id」:「2」, 「name」:「red」,「status」:「10」},{「id」:「5」,「name」:「blue」,「status」:「15」}]

顯然我的代碼只顯示第一個名字「白色」。我無法理解如何迭代JSONObject以顯示所有值。我在其他問題上嘗試了答案,但我無法將它們合併到我的代碼中。

回答

1

這是因爲你剛從JSONArray獲得第一個元素。 (索引0)

您應該遍歷JSONArray以獲取數組中的所有JSONObject

這樣,

JSONArray jArray = new JSONArray(str); 
int total=jArray.length(); 
for(int i=0;i<total;i++) { 
    JSONObject json = jArray.getJSONObject(i); // Replace 0 with i'th index. 
    // use this json object to iterate over individual objects. 
} 
0

Here的例子JSON解析和插入,更新,刪除或源從服務器獲取數據,你應該試試這個的!

快樂編碼!

0

您的代碼問題是Alok Patel所說的。但是我發現你的代碼的邏輯需要做一些改變來做你想做的事情(根據你發佈的示例json)。你在實際上是簡單數據的值上調用瞭解析方法,而你應該在jsonObjects上調用它。

我重構如下代碼做你想要什麼:

public class MainActivity extends Activity { 

TextView textview; 
JSONObject json = null; 
String str = ""; 
HttpResponse response; 
Context context; 
ProgressBar progressbar; 
Button button; 
JSONArray jArray; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    progressbar = (ProgressBar)findViewById(R.id.progressBar1); 
    textview = (TextView)findViewById(R.id.textView1); 
    button = (Button)findViewById(R.id.button1); 

    progressbar.setVisibility(View.GONE); 

    button.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

      progressbar.setVisibility(View.VISIBLE); 

      new GetTextViewData(context).execute(); 

     } 
    }); 
} 



private class GetTextViewData extends AsyncTask<Void, Void, Void> 
{ 
    public Context context; 
    Map<String,String> out = new Map<String, String>(); 

    public GetTextViewData(Context context) 
    { 
     this.context = context; 
    } 

    @Override 
    protected void onPreExecute() 
    { 
     super.onPreExecute(); 
    } 

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

     HttpClient myClient = new DefaultHttpClient(); 
     HttpPost myConnection = new HttpPost("http://192.168.1.9:80/test-androidex/send-data.php"); 

     try { 
      response = myClient.execute(myConnection); 
      str = EntityUtils.toString(response.getEntity(), "UTF-8"); 

     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 


     try{ 
      JSONArray jArray = new JSONArray(str); 
      int total=jArray.length(); 

      for(int i=0;i<total;i++) { 
       JSONObject json = jArray.getJSONObject(i); 
       parse(json, out); 
      } 

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

     catch (Exception e) 
     { 

      e.printStackTrace(); 
     } 
     return null; 
    } 
    protected void onPostExecute(Void result) 
    { 

    try { 
     // print "out" object to console here by iterating over its keys 
     // or do any needed process on it here. 
      textview.setText(json.getString("name")); 

     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     progressbar.setVisibility(View.GONE); 

    } 
Map<String,String> parse(JSONObject json , Map<String,String> out) throws JSONException{ 
    Iterator<String> keys = json.keys(); 
    while(keys.hasNext()){ 
     String key = keys.next(); 
     String val = null; 
     try{ 
      val = json.getString(key); 
     }catch(Exception e){ 

     } 

     if(val != null){ 
      out.put(key,val); 
     } 
    } 
    return out; 
} 
}