Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 619f9c0
Showing
10 changed files
with
628 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
build.num |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<project default="package"> | ||
<property name="version.num" value="1.0.0" /> | ||
<buildnumber file="build.num" /> | ||
<property name="lib.dir" value="/usr/local/android_sdk/platforms/android-7/" /> | ||
<property name="build.dir" value="./build"/> | ||
<property name="classes.dir" value="${build.dir}/classes"/> | ||
|
||
<path id="classpath"> | ||
<fileset dir="${lib.dir}" includes="**/*.jar" /> | ||
</path> | ||
|
||
<target name="compile"> | ||
<mkdir dir="${build.dir}" /> | ||
<mkdir dir="${classes.dir}" /> | ||
<javac srcdir="src" destdir="${classes.dir}" classpathref="classpath" includeantruntime="false"> | ||
<compilerarg value="-Xlint:unchecked"/> | ||
</javac> | ||
</target> | ||
|
||
<target name="jar" depends="compile"> | ||
<delete file="async-http.jar" /> | ||
<delete file="MANIFEST.MF" /> | ||
<manifest file="MANIFEST.MF"> | ||
<attribute name="Built-By" value="${user.name}" /> | ||
<attribute name="Implementation-Version" value="${version.num}-b${build.number}"/> | ||
</manifest> | ||
|
||
<jar destfile="async-http.jar" basedir="build/classes" includes="**/*.class" manifest="MANIFEST.MF" /> | ||
</target> | ||
|
||
<target name="clean"> | ||
<delete dir="build" /> | ||
<delete> | ||
<fileset dir="." includes="async-http.jar*"/> | ||
<fileset file="MANIFEST.MF"/> | ||
</delete> | ||
</target> | ||
|
||
<target name="package" depends="compile,jar" /> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import java.util.Locale; | ||
|
||
import com.loopj.android.http.AsyncHttpClient; | ||
import com.loopj.android.http.AsyncHttpRequest; | ||
import com.loopj.android.http.AsyncHttpParams; | ||
|
||
class ExampleRestClient { | ||
private static final String USER_AGENT = "Example Android Rest Client"; | ||
private static final String BASE_URL = "http://api.example.com/"; | ||
|
||
private static AsyncHttpClient client; | ||
|
||
static { | ||
client = new AsyncHttpClient(USER_AGENT); | ||
client.setCookieStore(new PersistentCookieStore(AndroidApplication.getContext())); | ||
} | ||
|
||
public static void get(String url, AsyncHttpParams params, AsyncHttpRequest.OnResponseHandler responseHandler) { | ||
client.get(getAbsoluteUrl(url), augmentParams(params), responseHandler); | ||
} | ||
|
||
public static void post(String url, AsyncHttpParams params, AsyncHttpRequest.OnResponseHandler responseHandler) { | ||
client.get(getAbsoluteUrl(url), augmentParams(params), responseHandler); | ||
} | ||
|
||
private static String getAbsoluteUrl(String relativeUrl) { | ||
return BASE_URL + relativeUrl; | ||
} | ||
|
||
private static AsyncHttpParams augmentParams(AsyncHttpParams params) { | ||
if(params == null) { | ||
params = new AsyncHttpParams(); | ||
} | ||
|
||
// Add locale info to every request | ||
Locale currentLocale = Locale.getDefault(); | ||
params.put("country_code", currentLocale.getISO3Country()); | ||
params.put("language_code", currentLocale.getISO3Language()); | ||
|
||
return params; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import com.loopj.android.http.AsyncHttpClient; | ||
import com.loopj.android.http.AsyncHttpRequest; | ||
|
||
class ExampleUsage { | ||
public static void makeRequest() { | ||
AsyncHttpClient client = new AsyncHttpClient("My User Agent"); | ||
client.get("http://www.google.com", new AsyncHttpRequest.OnResponseHandler() { | ||
@Override | ||
public void onSuccess(String response) { | ||
Log.d("ExampleUsage", response); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
package com.loopj.android.http; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.zip.GZIPInputStream; | ||
|
||
import org.apache.http.Header; | ||
import org.apache.http.HeaderElement; | ||
import org.apache.http.HttpEntity; | ||
import org.apache.http.HttpRequest; | ||
import org.apache.http.HttpRequestInterceptor; | ||
import org.apache.http.HttpResponse; | ||
import org.apache.http.HttpResponseInterceptor; | ||
import org.apache.http.HttpVersion; | ||
import org.apache.http.client.CookieStore; | ||
import org.apache.http.client.methods.HttpGet; | ||
import org.apache.http.client.methods.HttpPost; | ||
import org.apache.http.client.methods.HttpRequestBase; | ||
import org.apache.http.client.protocol.ClientContext; | ||
import org.apache.http.conn.params.ConnManagerParams; | ||
import org.apache.http.conn.params.ConnPerRouteBean; | ||
import org.apache.http.conn.scheme.PlainSocketFactory; | ||
import org.apache.http.conn.scheme.Scheme; | ||
import org.apache.http.conn.scheme.SchemeRegistry; | ||
import org.apache.http.conn.ssl.SSLSocketFactory; | ||
import org.apache.http.entity.HttpEntityWrapper; | ||
import org.apache.http.impl.client.DefaultHttpClient; | ||
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager; | ||
import org.apache.http.params.BasicHttpParams; | ||
import org.apache.http.params.HttpConnectionParams; | ||
import org.apache.http.params.HttpProtocolParams; | ||
import org.apache.http.protocol.BasicHttpContext; | ||
import org.apache.http.protocol.SyncBasicHttpContext; | ||
import org.apache.http.protocol.HttpContext; | ||
|
||
public class AsyncHttpClient { | ||
public static final int DEFAULT_MAX_CONNECTIONS = 4; | ||
public static final int DEFAULT_SOCKET_TIMEOUT = 30 * 1000; | ||
private static final String ENCODING = "UTF-8"; | ||
private static final String HEADER_ACCEPT_ENCODING = "Accept-Encoding"; | ||
private static final String ENCODING_GZIP = "gzip"; | ||
|
||
private static int maxConnections = DEFAULT_MAX_CONNECTIONS; | ||
private static int socketTimeout = DEFAULT_SOCKET_TIMEOUT; | ||
|
||
private DefaultHttpClient httpClient; | ||
private HttpContext httpContext; | ||
private ExecutorService threadPool; | ||
|
||
public AsyncHttpClient(String userAgent) { | ||
BasicHttpParams httpParams = new BasicHttpParams(); | ||
|
||
ConnManagerParams.setTimeout(httpParams, socketTimeout); | ||
ConnManagerParams.setMaxConnectionsPerRoute(httpParams, new ConnPerRouteBean(maxConnections)); | ||
ConnManagerParams.setMaxTotalConnections(httpParams, DEFAULT_MAX_CONNECTIONS); | ||
|
||
HttpConnectionParams.setSoTimeout(httpParams, socketTimeout); | ||
HttpConnectionParams.setTcpNoDelay(httpParams, true); | ||
|
||
HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1); | ||
HttpProtocolParams.setUserAgent(httpParams, userAgent); | ||
|
||
SchemeRegistry schemeRegistry = new SchemeRegistry(); | ||
schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); | ||
schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); | ||
ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(httpParams, schemeRegistry); | ||
|
||
threadPool = Executors.newCachedThreadPool(); | ||
httpContext = new SyncBasicHttpContext(new BasicHttpContext()); | ||
httpClient = new DefaultHttpClient(cm, httpParams); | ||
httpClient.addRequestInterceptor(new HttpRequestInterceptor() { | ||
public void process(HttpRequest request, HttpContext context) { | ||
if (!request.containsHeader(HEADER_ACCEPT_ENCODING)) { | ||
request.addHeader(HEADER_ACCEPT_ENCODING, ENCODING_GZIP); | ||
} | ||
} | ||
}); | ||
|
||
httpClient.addResponseInterceptor(new HttpResponseInterceptor() { | ||
public void process(HttpResponse response, HttpContext context) { | ||
final HttpEntity entity = response.getEntity(); | ||
final Header encoding = entity.getContentEncoding(); | ||
if (encoding != null) { | ||
for (HeaderElement element : encoding.getElements()) { | ||
if (element.getName().equalsIgnoreCase(ENCODING_GZIP)) { | ||
response.setEntity(new InflatingEntity(response.getEntity())); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
|
||
public void setCookieStore(CookieStore cookieStore) { | ||
httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); | ||
} | ||
|
||
public void get(String url, AsyncHttpRequest.OnResponseHandler responseHandler) { | ||
get(url, null, responseHandler); | ||
} | ||
|
||
public void get(String url, AsyncHttpParams params, AsyncHttpRequest.OnResponseHandler responseHandler) { | ||
// Build and append query string (utf8 url encoded) | ||
if(params != null) { | ||
String paramString = params.getParamString(); | ||
url += "?" + paramString; | ||
} | ||
|
||
// Fire up the request in a new thread | ||
executeAsyncRequest(new HttpGet(url), responseHandler); | ||
} | ||
|
||
public void post(String url, AsyncHttpRequest.OnResponseHandler responseHandler) { | ||
post(url, null, responseHandler); | ||
} | ||
|
||
public void post(String url, AsyncHttpParams params, AsyncHttpRequest.OnResponseHandler responseHandler) { | ||
// Build post object with params | ||
final HttpPost post = new HttpPost(url); | ||
if(params != null) { | ||
HttpEntity entity = params.getEntity(); | ||
if(entity != null){ | ||
post.setEntity(entity); | ||
} | ||
} | ||
|
||
// Fire up the request in a new thread | ||
executeAsyncRequest(post, responseHandler); | ||
} | ||
|
||
private void executeAsyncRequest(final HttpRequestBase request, final AsyncHttpRequest.OnResponseHandler responseHandler) { | ||
threadPool.execute(new AsyncHttpRequest(httpClient, httpContext, request, responseHandler)); | ||
} | ||
|
||
private static class InflatingEntity extends HttpEntityWrapper { | ||
public InflatingEntity(HttpEntity wrapped) { | ||
super(wrapped); | ||
} | ||
|
||
@Override | ||
public InputStream getContent() throws IOException { | ||
return new GZIPInputStream(wrappedEntity.getContent()); | ||
} | ||
|
||
@Override | ||
public long getContentLength() { | ||
return -1; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.loopj.android.http; | ||
|
||
import java.io.UnsupportedEncodingException; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
import org.apache.http.HttpEntity; | ||
import org.apache.http.client.entity.UrlEncodedFormEntity; | ||
import org.apache.http.client.utils.URLEncodedUtils; | ||
import org.apache.http.message.BasicNameValuePair; | ||
|
||
public class AsyncHttpParams { | ||
private static String ENCODING = "UTF-8"; | ||
|
||
private ConcurrentHashMap<String,String> urlParams; | ||
|
||
public AsyncHttpParams() { | ||
init(); | ||
} | ||
|
||
public AsyncHttpParams(String key, String value) { | ||
init(); | ||
put(key, value); | ||
} | ||
|
||
public void put(String key, String value){ | ||
urlParams.put(key,value); | ||
} | ||
|
||
public void remove(String key){ | ||
urlParams.remove(key); | ||
} | ||
|
||
public String getParamString() { | ||
return URLEncodedUtils.format(getParamsList(), ENCODING); | ||
} | ||
|
||
public HttpEntity getEntity() { | ||
HttpEntity entity = null; | ||
try { | ||
entity = new UrlEncodedFormEntity(getParamsList(), ENCODING); | ||
} catch (UnsupportedEncodingException e) { | ||
e.printStackTrace(); | ||
} | ||
return entity; | ||
} | ||
|
||
private void init(){ | ||
urlParams = new ConcurrentHashMap<String,String>(); | ||
} | ||
|
||
private List<BasicNameValuePair> getParamsList() { | ||
List<BasicNameValuePair> lparams = new LinkedList<BasicNameValuePair>(); | ||
|
||
for(ConcurrentHashMap.Entry<String, String> entry : urlParams.entrySet()) { | ||
lparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); | ||
} | ||
|
||
return lparams; | ||
} | ||
} |
Oops, something went wrong.