12
25/07/16 Presentation 1 Android HTTP library Surviving with Android

Android http library

Embed Size (px)

Citation preview

Page 1: Android http library

25/07/16 Presentation 1

Android HTTP library

Surviving with Android

Page 2: Android http library

Presentation 225/07/16

What is it?

When we develop an Android app, usually we have to connect to a remoteserver to get information. The connection usually is based on HTTP protocolbecause it provides a simple mechanism to transport information. Moreover,

almost all platforms provide a set of API based on HTTP and it is very commonthe scenario where an Android app needs to integrate with one of these

platforms. For these reasons, it is important to know how to choose the bestAndroid HTTP library. The best choice, of course, depends on the requirements

of our Android app, so we can provide some hints that help you to select thebest Android HTTP library according to our needs.

Page 3: Android http library

Presentation 325/07/16

Android HTTP Library:

The Android HTTP library is based on HttpUrlConnection. This Android HTTPlibrary supports HTTP and HTTPS protocol and it is the basic class to use when

handling HTTP connection. Before Android 6.0, there was another libraryshipped with Android SDK, called Android Apache HTTP. To use it:

android {

    useLibrary 'org.apache.http.legacy'

}

Page 4: Android http library

Presentation 425/07/16

Android HTTP Library:

The Android HTTP library is based on HttpUrlConnection. This Android HTTPlibrary supports HTTP and HTTPS protocol and it is the basic class to use when

handling HTTP connection. Before Android 6.0, there was another libraryshipped with Android SDK, called Android Apache HTTP. To use it:

android {

    useLibrary 'org.apache.http.legacy'

}

Page 5: Android http library

Presentation 525/07/16

HttpUrlConnection

HttpURLConnection connection = null;

try {

connection = (HttpURLConnection) (new URL(url)).openConnection();

connection.setRequestMethod("GET"); // or post

connection.connect();

InputStream is = connection.getInputStream();

}

Even if it is simple to use there is a drawback: we don’t have to make HTTP calls in UIthread. For this reason, the piece of code above must be wrapped in a different thread.Usually, the class used with HttpUrlConnection is the AsyncTask.

Page 6: Android http library

Presentation 625/07/16

Android HTTP Alternatives

Third-part libraries have these advantages:● Efficiency

● Parallel requests

● Caching system

● Non-blocking UI thread

● HTTP/2 support

At level of HTTP handling there are two main libraries:

● Volley

● OkHTTP

Page 7: Android http library

Presentation 725/07/16

Android Volley

Android Volley is library made by Google and offers veryinteresting features, as stated in its home page:

● Automatic scheduling of network requests

● Multiple concurrent network connections.

● Support for request prioritization

● Cancellation request API

Page 8: Android http library

Presentation 825/07/16

Android Volley: How to..

RequestQueue queue = Volley.newRequestQueue(ctx);

StringRequest req = new StringRequest(Request.Method.GET, url,

new Response.Listener<String>() {

@Override

public void onResponse(String data) {}

},

new Response.ErrorListener() {

@Override

OnError(...) {}

} );

queue.add(req);

Page 9: Android http library

Presentation 925/07/16

OkHTTP

OkHTTP is another interesting libraries to handle HTTP connections.

The main advantages provided by OkHTTP are:

●HTTP/2 support●Connection pooling●Response caching

Page 10: Android http library

Presentation 1025/07/16

OkHTTP

OkHttpClient client = new OkHttpClient();Request request = new Request.Builder().url(url).build();

client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Request request, IOException e) { // Handle error } @Override public void onResponse(Response response) throws IOException{ //handle response } });

Page 11: Android http library

Presentation 1125/07/16

Find more...

To find more information please visit

Android HTTP library: Handle HTTP, JSON, Images

Follow me @survivingwithan

Page 12: Android http library

25/07/16 Presentation 12

Thank you