Android Load JSON

Asked

Viewed 90 times

1

I need to get the information from a JSON, I created a php code that transforms the database into mysql in JSON https://portfoliounopar.000webhostapp.com/index.php, Now I need to take this information and put in a Recyclerview on android, I tried some ways but unsuccessfully so far, if anyone has any tips for this, a simple way, I have to use Retrofit? Volley? . Thank you.

1 answer

1

Google suggests Volley, as far as I can remember. For a very simple application, you can simply use:

import android.util.Log;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;


public class HttpRequestHelper {
    public static String getRawResult(String url) {
        HttpURLConnection c = null;
        try {
            URL u = new URL(url);
            c = (HttpURLConnection) u.openConnection();
            c.setRequestMethod("GET");
            c.setRequestProperty("Content-length", "0");
            c.setUseCaches(false);
            c.setAllowUserInteraction(false);
            c.setConnectTimeout(40000);
            c.setReadTimeout(40000);
            c.connect();
            int status = c.getResponseCode();

            switch (status) {
                case 200:
                case 201:
                    BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
                    StringBuilder sb = new StringBuilder();
                    String line;
                    while ((line = br.readLine()) != null) {
                        sb.append(line+"\n");
                    }
                    br.close();
                    return sb.toString();
            }

        } catch (MalformedURLException ex) {
            Log.i("suaTag", "HttpConnectionHelper error: " + ex.getMessage());
        } catch (IOException ex) {
            Log.i("suaTag", "HttpConnectionHelper error: " + ex.getMessage());
        } finally {
            if (c != null) {
                try {
                    c.disconnect();
                } catch (Exception ex) {
                    Log.i("suaTag", "HttpConnectionHelper error: " + ex.getMessage());
                }
            }
        }
        return null;
    }

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.