在Android的阅读的Json在web服务休息。 FileNotFoundException异常 [英] Reading Json in webservice Rest in Android. FileNotFoundException

查看:161
本文介绍了在Android的阅读的Json在web服务休息。 FileNotFoundException异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道为什么没有在Android版此JSON? urlJson

结果是Fi​​leNotFoundException异常,但在导航仪的工作原理。

编辑:

 公共静态字符串readUrl(字符串urlString)抛出异常{
        读者的BufferedReader = NULL;        尝试{
            网址URL =新的URL(urlString);
            读者=新的BufferedReader(新的InputStreamReader(url.openStream()));
            StringBuffer的缓冲区=新的StringBuffer();
            INT读;
            的char [] =字符新的char [1024];
            而((读= reader.read(字))!= -1)
                buffer.append(字符,0,读);            返回buffer.toString();
        } {最后
            如果(读者!= NULL)
                reader.close();
        }


解决方案

使用的BufferedReader是不是做这个任务的最佳方法。
很多更好地利用HttpClient的使用图书馆org.json下面的示例*;

 的HttpClient HttpClient的=新DefaultHttpClient();    HttpPost后=新HttpPost(HTTP://www.*****.com/json);        //执行HTTP POST请求
        HTT presponse响应= httpClient.execute(岗位);
        串respStr = EntityUtils.toString(response.getEntity());        JSONArray respJSON =新JSONArray(respStr);

Someone know why not works this json in android? urlJson

Result is FileNotFoundException, but on the navigator works.

edit:

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();
        }

解决方案

Use BufferedReader is not the best method to do this task. Is a lot better use HttpClient with following sample using the library org.json.*;

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);

这篇关于在Android的阅读的Json在web服务休息。 FileNotFoundException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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