如何将Json字符串转换为Android中的ListView

问题描述:

希望有人能够帮助我理解这一点,我完全迷失了方向,似乎无法摆脱这种困扰。如何将Json字符串转换为Android中的ListView

我有一个信息是直接从数据库中,此刻的应用程序,Android的连接到PHP页面,结果被打印上的应用,如:

{"offer":"Half Price Drinks", "Day":"Monday"}

{"offer":"Half Price Drinks", "Day":"Tuesday"}

我需要将其转换为列表视图,任何人都可以建议我如何做到这一点?

我已经把在连接到PHP页面,并在拉该信息的页面。

public class BarsActivity extends Activity { 

    TextView txt; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     // Create a crude view - this should really be set via the layout resources 
     // but since its an example saves declaring them in the XML. 
     LinearLayout rootLayout = new LinearLayout(getApplicationContext()); 
     txt = new TextView(getApplicationContext()); 
     rootLayout.addView(txt); 
     setContentView(rootLayout); 

     // Set the text and call the connect function. 
     txt.setText("Connecting..."); 
     //call the method to run the data retreival 
     txt.setText(getServerData(KEY_121)); 



    } 
    public static final String KEY_121 = "http://10.0.2.2/SocialCrawler/content.php"; //i use my real ip here 



    private String getServerData(String returnString) { 

     InputStream is = null; 

     String result = ""; 
     //the year data to send 
     ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
     nameValuePairs.add(new BasicNameValuePair("day","Monday")); 

     //http post 
     try{ 
       HttpClient httpclient = new DefaultHttpClient(); 
       HttpPost httppost = new HttpPost(KEY_121); 
       httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
       HttpResponse response = httpclient.execute(httppost); 
       HttpEntity entity = response.getEntity(); 
       is = entity.getContent(); 

     }catch(Exception e){ 
       Log.e("log_tag", "Error in http connection "+e.toString()); 
     } 

     //convert response to string 
     try{ 
       BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); 
       StringBuilder sb = new StringBuilder(); 
       String line = null; 
       while ((line = reader.readLine()) != null) { 
         sb.append(line + "\n"); 
       } 
       is.close(); 
       result=sb.toString(); 
     }catch(Exception e){ 
       Log.e("log_tag", "Error converting result "+e.toString()); 
     } 
     //parse json data 
     try{ 
       JSONArray jArray = new JSONArray(result); 
       for(int i=0;i<jArray.length();i++){ 
         JSONObject json_data = jArray.getJSONObject(i); 
         Log.i("log_tag","day: "+json_data.getString("day")+ 
           ", offer: "+json_data.getString("offer") 
         ); 
         //Get an output to the screen 
         returnString += "\n\t" + jArray.getJSONObject(i); 
       } 
     }catch(JSONException e){ 
       Log.e("log_tag", "Error parsing data "+e.toString()); 
     } 
     return returnString; 

    } 
} 
+3

老实说。在互联网上有像吨和大量的例子。 http://p-xr.com/android-tutorial-how-to-parse-read-json-data-into-a-android-listview/和http://mobile.dzone.com/news/android-tutorial如何解析和大量的在这个网站 – 2012-02-23 01:29:10

+0

数据如何映射到ListView?一个View会显示今日优惠,还是ListView会显示今日优惠和即将推出的优惠? – 2012-02-23 01:34:58

+0

它只显示那天的优惠清单,我有它,所以用户选择了一天,然后当天传递给PHP文件,该文件返回当天的优惠。 – niallian 2012-02-23 08:50:15

通过遍历所有键 - >值转换的JSON到一个ArrayList和使用ArrayAdapter