如何从php获取JSON数据并在android中显示到textview中?

如何从php获取JSON数据并在android中显示到textview中?,php,android,mysql,database,json,Php,Android,Mysql,Database,Json,我正在制作一个android程序,它将基于php网页上的JSON在TextView上显示数据 以下是已显示在php页面上的我的JSON示例: [{"ID":"1","temperature":"33","max_humidity":"33","min_humidity":"34","lowtide":"35","hightide":"35","sunrise":"500","sunset":"530","moonrise":"530","moonset":"540","illumination"

我正在制作一个android程序,它将基于php网页上的JSON在TextView上显示数据

以下是已显示在php页面上的我的JSON示例:

[{"ID":"1","temperature":"33","max_humidity":"33","min_humidity":"34","lowtide":"35","hightide":"35","sunrise":"500","sunset":"530","moonrise":"530","moonset":"540","illumination":"45%"}]
我想在我的一个文本视图中显示“温度:33”,等等

我试着在我的android程序中使用httpclient&string buffer获取我网页的源代码,然后只解析JSON文本,然后传递给变量,但这对我不起作用。我需要这个网页,因为我的数据库在那里(这是我的JSON的来源)


有什么非常简单的方法可以做到这一点吗?

Gson是解析json的好库:

Gson是解析json的好库:

好的,下面是您需要的。不需要第三方图书馆

try{
    String jsonStr = getFromWeb(); // jsonStr = {"ID":"1","temperature":"33","max_humidity":"33","min_humidity":"34","lowtide":"35","hightide":"35","sunrise":"500","sunset":"530","moonrise":"530","moonset":"540","illumination":"45%"}]
        JSONObject obj = new JSONObject(jsonStr);
        String temperature = obj.getString("temperature");
        TextView tv = (TextView)findViewById(R.id.yourtextviewid);
        tv.setText("Temperature: " + temperature);
    }catch (JSONException e) {
      e.printStackTrace();
    }

好的,这是你需要的。不需要第三方图书馆

try{
    String jsonStr = getFromWeb(); // jsonStr = {"ID":"1","temperature":"33","max_humidity":"33","min_humidity":"34","lowtide":"35","hightide":"35","sunrise":"500","sunset":"530","moonrise":"530","moonset":"540","illumination":"45%"}]
        JSONObject obj = new JSONObject(jsonStr);
        String temperature = obj.getString("temperature");
        TextView tv = (TextView)findViewById(R.id.yourtextviewid);
        tv.setText("Temperature: " + temperature);
    }catch (JSONException e) {
      e.printStackTrace();
    }

谢谢我已经搜索了好几天了,但是这个简洁的代码对我来说很有用很乐意帮忙。请投票并将此标记为答案。当然!一旦我得到15分,我就会投票表决,因为现在我不能:)好的。我对你的问题投了赞成票。您有+5次重复。谢谢!我已经搜索了好几天了,但是这个简洁的代码对我来说很有用很乐意帮忙。请投票并将此标记为答案。当然!一旦我得到15分,我就会投票表决,因为现在我不能:)好的。我对你的问题投了赞成票。你有+5次重复。