← Back to loopj/android-async-http

How to Deploy & Use loopj/android-async-http

Android Async HTTP Client - Deployment and Usage Guide

Prerequisites

  • Android Studio (version 3.0 or higher recommended)
  • Java Development Kit (JDK) (version 8 or higher)
  • Android SDK with API level 16 (Android 4.1) or higher
  • Gradle (version 4.1 or higher)
  • Android device or emulator with API level 16+ for testing

Installation

Clone the Repository

git clone https://github.com/android-async-http/android-async-http.git
cd android-async-http

Build the Library

# Build the library and sample app
./gradlew build

# Install the sample app on a connected device
./gradlew :sample:installDebug

Add to Your Project

Using Maven Central (Recommended)

Add to your build.gradle:

repositories {
    mavenCentral()
}

dependencies {
    implementation 'com.loopj.android:android-async-http:1.4.11'
}

Using Snapshot Repository

repositories {
    maven {
        url 'https://oss.sonatype.org/content/repositories/snapshots/'
    }
}

dependencies {
    implementation 'com.loopj.android:android-async-http:1.4.12-SNAPSHOT'
}

Configuration

Required Permissions

Add these permissions to your AndroidManifest.xml:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Optional Configuration

For SNI support on older Android devices, add Conscrypt to your dependencies:

implementation 'org.conscrypt:conscrypt-android:2.5.1'

Build & Run

Development

  1. Open the project in Android Studio
  2. Sync Gradle files
  3. Build and run the sample application to test functionality

Using in Your Application

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.AsyncHttpResponseHandler;

AsyncHttpClient client = new AsyncHttpClient();
RequestParams params = new RequestParams();
params.put("username", "james");
params.put("password", "123456");

client.post("https://api.example.com/login", params, new AsyncHttpResponseHandler() {
    @Override
    public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
        // Handle successful response
    }

    @Override
    public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
        // Handle failure
    }
});

Deployment

This library is designed to be included as a dependency in your Android application. Deployment involves:

  1. Publishing to Maven Central (for library maintainers)
  2. Including in your app via Gradle dependencies
  3. Building your Android app using standard Android deployment methods

For library maintainers:

  • Ensure proper versioning in build.gradle
  • Update the CHANGELOG.md with release notes
  • Publish to Maven Central using standard Maven deployment procedures

Troubleshooting

Common Issues and Solutions

1. Network Operations on Main Thread

Issue: Network operations on main thread causing NetworkOnMainThreadException

Solution: Always use AsyncHttpClient for network operations. The library handles threading automatically.

// ✅ Correct - Asynchronous
client.get(url, new AsyncHttpResponseHandler() {
    // Callback methods
});

// ❌ Incorrect - Synchronous (will crash)
HttpResponse response = client.get(url);

2. SSL/TLS Handshake Issues

Issue: SSL handshake failures on older Android devices

Solution: Add Conscrypt for SNI support:

implementation 'org.conscrypt:conscrypt-android:2.5.1'

3. Memory Issues with Large Files

Issue: OutOfMemoryError when uploading large files

Solution: Use streaming upload with JsonStreamerEntity:

JsonStreamerEntity entity = new JsonStreamerEntity();
entity.addPart("file", new File("largefile.jpg"));
client.post(context, url, entity, "application/json", responseHandler);

4. Timeout Issues

Issue: Requests timing out on slow networks

Solution: Configure timeout settings:

AsyncHttpClient client = new AsyncHttpClient();
client.setTimeout(30000); // 30 seconds timeout

5. GZIP Response Issues

Issue: GZIP responses not being decoded

Solution: The library automatically handles GZIP decoding. Ensure your server sends proper Content-Encoding: gzip headers.

6. Cookie Persistence

Issue: Cookies not persisting between app launches

Solution: Use PersistentCookieStore:

PersistentCookieStore myCookieStore = new PersistentCookieStore(context);
client.setCookieStore(myCookieStore);

7. Request Cancellation

Issue: Need to cancel ongoing requests

Solution: Use RequestHandle to cancel requests:

RequestHandle handle = client.get(url, responseHandler);
// Later, to cancel:
handle.cancel(true);

Debug Mode

Enable debug logging for troubleshooting:

client.setLoggingLevel(Log.DEBUG);
Log.d("AsyncHttpClient", "Debug logging enabled");

Testing

Use the included sample application to test various scenarios:

  • Clone the repository
  • Run ./gradlew :sample:installDebug
  • Test different request types and configurations

For additional help, refer to the project documentation or check the GitHub issues.