Search code examples
androidjsonurlandroid-asynctaskopendata

Android Json Get URL STOPPED


I just started the topic to get data from a JSON OpenData and visualize via my phone.

I followed this tutorial and it has worked all :)

https://www.learn2crack.com/2013/10/android-asynctask-json-parsing-example.html The url whew i get the datas is :

http:// + api.learn2crack.com/android/json/ (sorry for that, I don't have a good reputation :) )

Then I wanted to try a Opendata me and my android application stops, the url is:

http://ckan.opendata.nets.upf.edu/storage/f/2013-11-30T16%3A49%3A59.118Z/london.json

You can see it's the same and I only change the name of URL in the code. You know if the problem is because of the OpenData? and I need some permission? Because when I execute the second part my app stopped


Solution

  • Here, this will work. I am using Strict Policy but normally you should use Async. Please google this as to why we should use Async instead of Strict Policy. This is irrelevant here

    When i am using HttpPost to get your json from url, i am getting these errors :-

    405 Method Not Allowed

    The method POST is not allowed for this resource.

    You cannot POST a file

    so i am using HttpGet :-

            String url = "http://ckan.opendata.nets.upf.edu/storage/f/2013-11-30T16:49:59.118Z/london.json";
    
        try{
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    
            StrictMode.setThreadPolicy(policy);
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            //HttpPost httpPost = new HttpPost(url);
            HttpGet httpGet = new HttpGet(url);
            HttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();
     BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            JSONObject jObj = new JSONObject(sb.toString());
            JSONArray json2 = json.getJSONArray("user");
    
            for (int i = 0; i < json2.length(); i++) {
               JSONObject c = json2.getJSONObject(i);
        }
    }
    catch (Exception e) {
                e.printStackTrace();
            }