Consuming a JSON from android What's wrong? [closed]


I'm trying to get a Json from a web resource and convert it to string to show it on the screen. I am using android Studio for this and I can not consume that resource. Any idea that is wrong PS: In the manifests I have added the INTERNET and ACCESS_NETWORK_STATE permissions.

public void getData(String URL) throws Exception {
    URL url = new URL(URL);
    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

    InputStream in = new BufferedInputStream(urlConnection.getInputStream());
    JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));

    while (reader.hasNext()){
asked by alberto 18.10.2016 в 13:11

2 answers


You can not make requests to the internet from the main thread, you can do it from an asyntask: The following link will be useful: link

PS: you usually put the error that shows you logcat.

answered by 18.10.2016 в 13:37

The method should return the resource "String" or whatever you want and not be "void" if you want to use the result.

    // read the JSON results into a string
    String jsonResult = in.readLine();
    return jsonResult;

It is also convenient that you do this activity in a secondary thread or thread.

answered by 18.10.2016 в 13:38