【问题标题】:Reading Json in webservice Rest in Android. FileNotFoundException在 Android 的 Web 服务 Rest 中读取 Json。 FileNotFoundException
【发布时间】:2012-08-15 22:45:35
【问题描述】:

有人知道为什么不能在 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();
        }

【问题讨论】:

    标签: android json web-services rest filenotfoundexception


    【解决方案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);
    

    【讨论】:

      【解决方案2】:

      格式错误。 使用 {"name": "string", "...": "..."}

      【讨论】:

        【解决方案3】:

        问题已解决,我正在使用 spring,并在服务中删除了它

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

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

        现在可以了!

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-11-06
          • 1970-01-01
          • 1970-01-01
          • 2014-12-23
          相关资源
          最近更新 更多