【问题标题】:How to convert Json string to ListView in Android如何在Android中将Json字符串转换为ListView
【发布时间】:2012-02-23 01:23:18
【问题描述】:

希望有人能帮助我理解这一点,我完全迷失了,似乎根本无法理解这一点。

我有一个从数据库中提取信息的应用程序,此时,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; 

    }
}

【问题讨论】:

  • 老实说。互联网上有很多例子。 p-xr.com/…mobile.dzone.com/news/android-tutorial-how-parse 和很多在这个网站上
  • 数据如何映射到 ListView?一个 View 会显示今天的优惠,还是 ListView 会显示今天的优惠和即将推出的优惠?
  • 它只会显示当天的报价列表,我有它,所以用户选择了一天,然后将这一天传递给 PHP 文件,该文件返回当天的报价。
  • 也许有人可以帮助我完成教程,我已经获取了源代码并尝试输入我的信息,我是否正确地说唯一需要更改的是 url Main.java 上的文件的 id、eqid 和幅度,它们必须更改为我数据库中的变量吗?

标签: php android sql database json


【解决方案1】:

通过迭代所有键->值并使用 ArrayAdapter 将 json 转换为 ArrayList

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-07
    • 2011-03-23
    • 1970-01-01
    相关资源
    最近更新 更多