duanhui4160 2014-01-22 09:05
浏览 22
已采纳

如何从php中获取android中的简单json数据

I am totally new to java/android . I have searched many tutorials over the internet. But those didnt helped me. So I have posted this problem here.

I have simple json data in php like below,

[{"title":"re3","notice":"etrf4w"},{"title":"vfdxg","notice":"fdgd"},  {"title":"notice1","notice":"details..."}]

I want to show this json data in android as a plain text like below,

Title:re3

notice: etrf4w ..................... ...................

How could I do that. please help me.

  • 写回答

4条回答 默认 最新

  • duanlaican1849 2014-01-22 09:12
    关注

    Here is the exmaple

    private Handler handler = new Handler(){
    
        @Override
        public void handleMessage(Message msg) {
            switch(msg.what){
                case 1:{
                    showList();
                }break;
            }
        }
    
    };
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        /*
        Your initialization code here
        */
    
        new Thread(){
    
            @Override
            public void run() {
                String resp = loadURLData(JSON_URL);
    
                MainActivity.this.items = new ArrayList<Item>();
    
                try{
                    JSONArray items = new JSONArray(resp);
    
                    for(int i = 0; i < items.length(); i++){
                        JSONObject item = items.getJSONObject(i);
    
                        String title = item.getString("title");
                        String notice = item.getString("notice");
    
                        Item it = new Item(title, notice);
                    }
    
                    handler.sendEmptyMessage(1);
                }catch(JSONException jSONEx){
                }
            }
    
        }.start();
    }
    
    private void showList(){
        ListAdapter adapter = new MyAdapter(this, items);
        listView.setAdapter(adapter);
    }
    
    private static String loadURLData(String msgsUrl){
        try{
            URL url = new URL(msgsUrl); 
            URLConnection conn = url.openConnection();
            InputStreamReader streamReader = new InputStreamReader(conn.getInputStream());
    
            BufferedReader br = new BufferedReader(streamReader);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while((line = br.readLine()) != null) {
                sb.append(line);sb.append("
    ");
            }
            br.close();
            String resp = sb.toString();
    
            return resp;
        }catch(IOException iOEx){
            return "";
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式