检索JSON的URL在Android [英] Retrieving JSON from URL on Android

查看:114
本文介绍了检索JSON的URL在Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在文本模式下我的手机应用程序下载的内容完美。下面是一个code做到这一点。我叫通讯类和exectueHttpGet:

My phone APP downloads content perfectly in a text mode. Below is a code to do that. I call Communicator class and exectueHttpGet:

URL_Data =新通讯()executeHttpGet(一些URL);

public class Communicator {
public String executeHttpGet(String URL) throws Exception 
{
    BufferedReader in = null;
    try 
    {
        HttpClient client = new DefaultHttpClient();
        client.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "android");
        HttpGet request = new HttpGet();
        request.setHeader("Content-Type", "text/plain; charset=utf-8");
        request.setURI(new URI(URL));
        HttpResponse response = client.execute(request);
        in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

        StringBuffer sb = new StringBuffer("");
        String line = "";

        String NL = System.getProperty("line.separator");
        while ((line = in.readLine()) != null) 
        {
            sb.append(line + NL);
        }
        in.close();
        String page = sb.toString();
        //System.out.println(page);
        return page;
    } 
    finally 
    {
        if (in != null) 
        {
            try 
            {
                in.close();
            } 
            catch (IOException e)    
            {
                Log.d("BBB", e.toString());
            }
        }
    }
}
}

我recive是这个(来源$ C ​​$网址C):

What I recive is this (source code of URL):

[{"id_country":"3","country":"AAA"},{"id_country":"8","country":"BBB"},
{"id_country":"66","country":"CCC"},{"id_country":"14","country":"DDD"},
{"id_country":"16","country":"EEE"},{"id_country":"19","country":"FFF"},
{"id_country":"24","country":"GGG"},{"id_country":"33","country":"HHH"},
{"id_country":"39","country":"III"},{"id_country":"44","country":"JJJ"},
{"id_country":"45","country":"KKK"},{"id_country":"51","country":"LLL"},
{"id_country":"54","country":"MMM"},{"id_country":"55","country":"NNN"},
{"id_country":"57","country":"OOO"},{"id_country":"58","country":"PPP"},
{"id_country":"63","country":"RRR"},{"id_country":"65","country":"SSS"}]

这反应是一个字符串。在服务器时,它输出(用PHP)的JSON对象,现在在我的Andr​​oid的PHP我想transfrom该字符串JSON。这可能吗?

This response is a String. On server it is outputted (with PHP) as JSON object and now in my Android PHP I want to transfrom this string to JSON. Is this possible?

推荐答案

你能拿到什么是一系列由字符的InputStream 你追加到的StringBuffer 并转换为字符串在结束 - 这样字符串的结果是OK:)

What you receive is a series of characters from the InputStream that you append to a StringBuffer and convert to String at the end - so the result of String is ok :)

您需要的是后处理通过 org.json此字符串。* 类,如

What you want is to post-process this String via org.json.* classes like

String page = new Communicator().executeHttpGet("Some URL");
JSONObject jsonObject = new JSONObject(page);

,然后在的JSONObject 工作。当你接收到的数据是一个数组,你其实可以说

and then work on jsonObject. As the data you receive is an array, you can actually say

String page = new Communicator().executeHttpGet("Some URL");
JSONArray jsonArray = new JSONArray(page);
for (int i = 0 ; i < jsonArray.length(); i++ ) {
  JSONObject entry = jsonArray.get(i);
  // now get the data from each entry
}

这篇关于检索JSON的URL在Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆