2016-08-13 80 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; 
} 
}