I seem to be getting intermittent SocketTimeout exceptions when either calling GET or POST using the loopj library for Android.
However this error only seems to be an issue on certain versions of android. That being said I've removed the SSL certificate setup I had on my api class and I am now just calling standard http get/post.
Errors that are being thrown are intermittently:
java.net.SocketTimeoutException: Read timed out
or
javax.net.ssl.SSLException: Write error: ssl=0x6118a8: I/O error during system call, Broken pipe
I have checked the error log on apache and i am getting:
client prematurely closed connection, so upstream connection is closed too while sending request to upstream
I am not sure if this is an issue at all?
import java.math.BigInteger;
import java.security.KeyStore;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import android.util.Log;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
public class Api {
private static String api_url = "http://myurl.co.uk/api";
private static VenueManager venueManager;
private static final String BASE_URL = "http://myurl.co.uk/api";
private static boolean success = false;
private static AsyncHttpClient client = null;
public static final String api_key = "API_KEY";
public static final String api_secret = "API_SECRET";
static final int DEFAULT_TIMEOUT = 20 * 1000;
// public static AsyncHttpClient get_client()
// {
// if(client != null)
// {
// return client;
// }
// else
// {
// client = new AsyncHttpClient();
// try
// {
// KeyStore trustStore = //KeyStore.getInstance(KeyStore.getDefaultType());
// trustStore.load(null, null);
// SocketFactory sf = new SocketFactory(trustStore);
// //sf.setHostnameVerifier(SocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
// client.setSSLSocketFactory(sf);
// client.setTimeout(DEFAULT_TIMEOUT);
// }
// catch (Exception e)
// {
// Log.v("API", e.toString());
// }
//
// return client;
// }
// }
public static String get_api_url()
{
return api_url;
}
public static String search_venues(double lat, double lng)
{
return get_api_url() + "/venues?lat=" + lat + "&lng=" + lng + "&radius=1.0";
}
public static String post_review()
{
return get_api_url() + "/review";
}
private static String getAbsoluteUrl(String relativeUrl) {
return BASE_URL + relativeUrl;
}
public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
client = new AsyncHttpClient();
client.setMaxRetriesAndTimeout(5, DEFAULT_TIMEOUT);
client.get(getAbsoluteUrl(url), params, responseHandler);
}
public static void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
client = new AsyncHttpClient();
client.setMaxRetriesAndTimeout(5, DEFAULT_TIMEOUT);
client.post(getAbsoluteUrl(url), params, responseHandler);
}
public static String sha1(String s) {
MessageDigest digest = null;
try
{
digest = MessageDigest.getInstance("SHA-1");
}
catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
digest.reset();
byte[] data = digest.digest(s.getBytes());
return String.format("%0" + (data.length*2) + "X", new BigInteger(1, data));
}
}
Above is my API class. Below is a method I call within an activity
public void check_survey()
{
RequestParams params = new RequestParams();
params.put("email", sharedPreferences.getString("email", null));
params.put("password", sharedPreferences.getString("password", null));
params.put("api_key", Api.api_key);
Api.get("/survey", params, new AsyncHttpResponseHandler() {
@Override
public void onFailure(Throwable e) {
Toast.makeText(HomeActivity.this, e.toString(), Toast.LENGTH_LONG).show();
loading.dismiss();
}
@Override
public void onFailure(Throwable e, String response) {
loading.dismiss();
}
@Override
public void onFinish() {
loading.dismiss();
}
@Override
public void onSuccess(String response) {
try
{
JSONObject result = new JSONObject(response);
JSONObject meta = result.getJSONObject("meta");
if(meta.getBoolean("success"))
{
if(result.getString("response").equals("true"))
{
// do something
}
}
else
{
// display errors
}
}
catch(Exception e)
{
Log.v("Error", e.toString());
}
loading.dismiss();
}
});
}
I'm really at a loose end as to why this is happening.