Android Asynchronous HTTP Client
Android Asynchronous HTTP Client
Android Asynchronous HTTP Client
com/android-async-http/
James Smith
Overview
If you are also looking for a great android crash reporting service, I’d also recommend
checking out my company, Bugsnag.
Features
1 de 8 25/04/2017 13:57
Android Asynchronous Http Client http://loopj.com/android-async-http/
BaseJsonHttpResponseHandler
Support for SAX parser with SaxAsyncHttpResponseHandler
Support for languages and content encodings, not just UTF-8
Instagram
Instagram is the #1 photo app on android, with over 10million users
Pinterest
Popular online pinboard. Organize and share things you love.
Heyzap
Social game discovery app with millions of users
Pose
Pose is the #1 fashion app for sharing and discovering new styles
dependencies {
compile 'com.loopj.android:android-async-http:1.4.9'
}
import com.loopj.android.http.*;
@Override
public void onStart() {
// called before request is started
}
2 de 8 25/04/2017 13:57
Android Asynchronous Http Client http://loopj.com/android-async-http/
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] response) {
// called when response HTTP status is "200 OK"
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] errorResponse,
// called when response HTTP status is "4XX" (eg. 401, 403, 404)
}
@Override
public void onRetry(int retryNo) {
// called when request is retried
}
});
In this example, we’ll make a h p client class with static accessors to make it easy to
communicate with Twi er’s API.
import com.loopj.android.http.*;
This then makes it very easy to work with the Twi er API in your code:
import org.json.*;
import com.loopj.android.http.*;
class TwitterRestClientUsage {
public void getPublicTimeline() throws JSONException {
TwitterRestClient.get("statuses/public_timeline.json", null, new JsonHttpResponseHandl
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response
// If the response is JSONObject instead of expected JSONArray
}
@Override
public void onSuccess(int statusCode, Header[] headers, JSONArray timeline
// Pull out the first event on the public timeline
JSONObject firstEvent = timeline.get(0);
String tweetText = firstEvent.getString("text");
3 de 8 25/04/2017 13:57
Android Asynchronous Http Client http://loopj.com/android-async-http/
This is extremely useful if you want to use cookies to manage authentication sessions,
since the user will remain logged in even a er closing and re-opening your app.
Any cookies received from servers will now be stored in the persistent cookie store.
To add your own cookies to the store, simply construct a new cookie and call
addCookie:
The RequestParams class is used to add optional GET or POST parameters to your
requests. RequestParams can be built and constructed in various ways:
4 de 8 25/04/2017 13:57
Android Asynchronous Http Client http://loopj.com/android-async-http/
5 de 8 25/04/2017 13:57
Android Asynchronous Http Client http://loopj.com/android-async-http/
Some requests may need username/password credentials when dealing with API
services that use HTTP Basic Access Authentication requests. You can use the method
setBasicAuth() to provide your credentials.
Set username/password for any host and realm for a particular request. By default the
Authentication Scope is for any host, port and realm.
Testing on device
You can test the library on real device or emulator using provided Sample Application.
Sample application implements all important functions of library, you can use it as
source of inspiration.
To run sample application, clone the android-async-h p github repository and run
command in it’s root:
gradle :sample:installDebug
Which will install Sample application on connected device, all examples do work
6 de 8 25/04/2017 13:57
Android Asynchronous Http Client http://loopj.com/android-async-http/
To build a .jar file from source, first make a clone of the android-async-h p github
repository. Then you have to have installed Android SDK and Gradle buildscript, then
just run:
gradle :library:jarRelease
Please report any bugs or feature requests on the github issues page for this project
here:
h ps://github.com/loopj/android-async-h p/issues
Luciano Vi i (h ps://github.com/xAnubiSx)
Collaborated on Sample Application
7 de 8 25/04/2017 13:57
Android Asynchronous Http Client http://loopj.com/android-async-http/
And many others, contributions are listed in each file in license header. You can also
find contributors by looking on project commits, issues and pull-requests on Github
License
h ps://www.apache.org/licenses/LICENSE-2.0
Iʹm the co-founder of Bugsnag with Simon Maynard, and from 2009 to 2012 I led up
the product team as CTO of Heyzap.
Follow @loopj
8 de 8 25/04/2017 13:57