I’m facing a problem with my Android app connecting to a Django REST API. I used OpenAPI generator to create the Android client from my API schema. I’ve set up the internet permissions in the manifest file:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
In my ImageApi
class, I’ve set the base path to http://10.0.2.2:8000
for the emulator. When I try to fetch images, I get a TimeoutException
. Here’s a snippet from the error log:
Caused by: java.util.concurrent.TimeoutException
at com.android.volley.toolbox.RequestFuture.doGet(RequestFuture.java:128)
at com.android.volley.toolbox.RequestFuture.get(RequestFuture.java:97)
at client.ApiInvoker.invokeAPI(ApiInvoker.java:372)
at client.api.ImageApi.imageList(ImageApi.java:347)
at com.example.myapp.MainActivity.testApi(MainActivity.java:53)
I’ve tried different IP addresses and ports, including localhost and 10.0.2.2 with port 80. Django is set up to run on 0.0.0.0:8000 with ALLOWED_HOSTS = ["*"]
. Interestingly, I can access the API through the mobile browser. Any ideas on what might be causing this connection timeout?
hey, have u tried checkin ur android studio logcat for any specific error messages? sometimes theres more info there. also, make sure ur django server is actually runnin and listenin on the right port. u could try python manage.py runserver 0.0.0.0:8000
to make sure its accessible from outside. good luck!
Have you considered the possibility of a firewall or antivirus software blocking the connection? Sometimes, these security measures can interfere with local development setups. Try temporarily disabling them to see if it resolves the issue.
Another thing to check is your Volley configuration. The default timeout might be too short. You can increase it by setting custom retry and timeout policies:
RequestQueue queue = Volley.newRequestQueue(this);
int MY_SOCKET_TIMEOUT_MS = 50000;
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener() {
@Override
public void onResponse(String response) {
// Handle the response
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Handle the error
}
});
stringRequest.setRetryPolicy(new DefaultRetryPolicy(
MY_SOCKET_TIMEOUT_MS,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
queue.add(stringRequest);
This extends the timeout to 50 seconds, which might help if your server is slow to respond.
I’ve encountered similar issues when connecting Android clients to Django REST APIs. One thing that often gets overlooked is the network security configuration. Since Android 9 (API level 28), cleartext traffic is blocked by default, which can cause timeouts.
Try adding a network security configuration file to your Android project. Create a new XML file in res/xml/ called network_security_config.xml with this content:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
</network-security-config>
Then, reference this in your AndroidManifest.xml:
<application
...
android:networkSecurityConfig="@xml/network_security_config">
This allows cleartext traffic, which might resolve your timeout issue. However, remember this is not secure for production. For a real app, you’d want to set up HTTPS.
Also, double-check that your Django server is actually running and accessible from the Android emulator. You can try using tools like cURL or Postman to test the API endpoints directly.