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条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?