doucong8553 2012-07-20 12:46
浏览 88
已采纳

如何从php服务器获取json数据到android手机

I am having an application in which i want to get json data from a php web server to android mobile. what I am having is a url and hitting that url gives me the json data like
{"items":[{"latitude":"420","longitude":"421"}]}. But I want to retrieve this json format in my android mobile and get the values of latitude and longitude from json format.

how can we get that on android mobile..?

Thanks in advance..

  • 写回答

4条回答 默认 最新

  • dongluan0020 2012-07-20 12:49
    关注

    First do URL Connection

    String parsedString = "";
    
        try {
    
            URL url = new URL(yourURL);
            URLConnection conn = url.openConnection();
    
            HttpURLConnection httpConn = (HttpURLConnection) conn;
            httpConn.setAllowUserInteraction(false);
            httpConn.setInstanceFollowRedirects(true);
            httpConn.setRequestMethod("GET");
            httpConn.connect();
    
            InputStream is = httpConn.getInputStream();
            parsedString = convertinputStreamToString(is);
    
        } catch (Exception e) {
            e.printStackTrace();
        }
    

    JSON String

    {
    "result": "success",
    "countryCodeList":
    [
      {"countryCode":"00","countryName":"World Wide"},
      {"countryCode":"kr","countryName":"Korea"}
    ] 
    }
    

    Here below I am fetching country details

    JSONObject json = new JSONObject(jsonstring);
    JSONArray nameArray = json.names();
    JSONArray valArray = json.toJSONArray(nameArray);
    
    JSONArray valArray1 = valArray.getJSONArray(1);
    
    valArray1.toString().replace("[", "");
    valArray1.toString().replace("]", "");
    
    int len = valArray1.length();
    
    for (int i = 0; i < valArray1.length(); i++) {
    
     Country country = new Country();
     JSONObject arr = valArray1.getJSONObject(i);
     country.setCountryCode(arr.getString("countryCode"));                        
     country.setCountryName(arr.getString("countryName"));
     arrCountries.add(country);
    }
    
    
    
    
    public static String convertinputStreamToString(InputStream ists)
            throws IOException {
        if (ists != null) {
            StringBuilder sb = new StringBuilder();
            String line;
    
            try {
                BufferedReader r1 = new BufferedReader(new InputStreamReader(
                        ists, "UTF-8"));
                while ((line = r1.readLine()) != null) {
                    sb.append(line).append("
    ");
                }
            } finally {
                ists.close();
            }
            return sb.toString();
        } else {
            return "";
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧