【问题标题】:How to get JSON data from php and display into textview in android? [closed]如何从 php 获取 JSON 数据并在 android 中显示到 textview 中? [关闭]
【发布时间】:2012-11-27 11:45:56
【问题描述】:

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

这是我的 JSON 示例,它已经显示在 php 页面上:

[{"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”等。

我尝试使用 httpclient 和字符串缓冲区在我的 android 程序中获取我的网页的源代码,然后只解析 JSON 文本然后传递给一个变量,但这对我不起作用。我需要该网页,因为我的数据库在那里(这是我的 JSON 的来源)

有什么非常简单的方法吗?

【问题讨论】:

    标签: php android mysql database json


    【解决方案1】:

    好的,这就是你需要的。不需要第三方库。

    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 次代表。
    【解决方案2】:

    Gson 是一个很棒的 json 解析库:http://code.google.com/p/google-gson/

    【讨论】:

      猜你喜欢
      • 2017-12-21
      • 2013-08-16
      • 2013-03-27
      • 1970-01-01
      • 1970-01-01
      • 2017-12-20
      • 2012-12-15
      • 2017-01-31
      • 2019-04-02
      相关资源
      最近更新 更多