1

有人知道为什么不能在 android 中使用这个 json 吗?urlJson

结果是 FileNotFoundException,但在导航器上有效。

编辑:

public static String readUrl(String urlString) throws Exception {
        BufferedReader reader = null;

        try{
            URL url = new URL(urlString);
            reader = new BufferedReader(new InputStreamReader (url.openStream()));
            StringBuffer buffer = new StringBuffer();
            int read;
            char[]chars = new char[1024];
            while ((read = reader.read(chars)) != -1)
                buffer.append(chars, 0, read); 

            return buffer.toString();
        } finally {
            if (reader != null)
                reader.close();
        }
4

3 回答 3

1

使用 BufferedReader 不是完成此任务的最佳方法。使用库 org.json.* 将 HttpClient 与以下示例一起使用会更好;

HttpClient httpClient = new DefaultHttpClient();

    HttpPost post = new HttpPost("http://www.*****.com/json");

        // Execute HTTP Post Request
        HttpResponse response = httpClient.execute(post);
        String respStr = EntityUtils.toString(response.getEntity());

        JSONArray respJSON = new JSONArray(respStr);
于 2012-08-16T10:55:10.973 回答
0

格式不对。使用 {"name": "string", "...": "..."}

于 2012-08-16T10:00:05.930 回答
0

问题解决了,我正在使用弹簧,我在服务中删除了它

@RequestMapping(value="/categorias", method = RequestMethod.GET,headers="Accept=application/json")
    public @ResponseBody Portada getCategorias() {

@RequestMapping(value="/categorias", method = RequestMethod.GET)
    public @ResponseBody Portada getCategorias() {

现在有效!

于 2012-08-16T11:26:13.363 回答