Android Upload JSON

1

I need to get information from a JSON, I created a code in php that transforms the database into mySQL into JSON link

asked by anonymous 12.09.2017 / 15:54

1 answer

0

Google suggests volleyball, 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;
    }

}
    
12.09.2017 / 16:18