Best practice for getting json from url in api 23

I use compileSdk 23version 23 with library support.

I used the httplegacy library (I copied it to the app / libs folder from androidSdk / android-23 / optional / org.apache.http.legacy.jar) and in gradle I put:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
}

To download this library.

In my Connection class, I have a way to load an instance DefaultHttpClientlike this:

private static HttpClient getClient(){
    HttpParams httpParameters = new BasicHttpParams();
    int timeoutConnection = 3000;
    HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
    int timeoutSocket = 3000;
    HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

    DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);

    return httpClient;
}

But Android Studio says all classes apache.httpare out of date.

What can I use to follow best practices?

+4
source share
2 answers

, . :

HTTP- Apache. Android 2.3 ( API 9) , HttpURLConnection. API

HttpURLConnection:

   URL url = new URL("http://www.android.com/");
   HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
   try {
     InputStream in = new BufferedInputStream(urlConnection.getInputStream());
     readStream(in);
    finally {
     urlConnection.disconnect();
   }
 }

- . Fuel Kotlin ( Java) Http-request Java-. HttpURLConnection .

Http-Request:

HttpRequest request = HttpRequest.get("http://google.com");
String body = request.body();
int code = request.code();

Fuel:

Fuel.get("http://httpbin.org/get", params).responseString(new Handler<String>() {
    @Override
    public void failure(Request request, Response response, FuelError error) {
        //do something when it is failure
    }

    @Override
    public void success(Request request, Response response, String data) {
        //do something when it is successful
    }
});

: Fuel , Http-Request .

+4

:

import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

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

public class JSONParser {

    static InputStream is = null;
    static JSONObject json = null;
    static String output = "";

    // constructor
    public JSONParser() {

    }

    public JSONObject getJSONFromUrl(String url, List params) {
        URL _url;
        HttpURLConnection urlConnection;

        try {
            _url = new URL(url);
            urlConnection = (HttpURLConnection) _url.openConnection();
        }
        catch (MalformedURLException e) {
            Log.e("JSON Parser", "Error due to a malformed URL " + e.toString());
            return null;
        }
        catch (IOException e) {
            Log.e("JSON Parser", "IO error " + e.toString());
            return null;
        }

        try {
            is = new BufferedInputStream(urlConnection.getInputStream());
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            StringBuilder total = new StringBuilder(is.available());
            String line;
            while ((line = reader.readLine()) != null) {
                total.append(line).append('\n');
            }
            output = total.toString();
        }
        catch (IOException e) {
            Log.e("JSON Parser", "IO error " + e.toString());
            return null;
        }
        finally{
            urlConnection.disconnect();
        }

        try {
            json = new JSONObject(output);
        }
        catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        return json;
    }
}

, {"content": "hello world"}, :

JSONParser jsonParser = new JSONParser();
JSONObject payload = jsonParser.getJSONFromUrl(
        "http://yourdomain.com/path/to/your/api",
        null);
System.out.println(payload.get("content");

"hello world" .

+3

Source: https://habr.com/ru/post/1607152/


All Articles