Reading Json in webservice Rest in Android. FileNotFoundException

孤者浪人 提交于 2019-12-24 12:58:34

问题


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

回答1:


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



回答2:


The format is wrong. Use {"name": "string", "...": "..."}




回答3:


Problem solved, I'm using spring and I removed this in the service

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

to

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

Now works!



来源:https://stackoverflow.com/questions/11978412/reading-json-in-webservice-rest-in-android-filenotfoundexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!