I am building an app that connects to a web server and fetches data as JSON. We are facing a problem that is intermittent that is the connection refused occurs once in a while not always. I followed - Android HTTP Connection refused and a few others which did not seem relevent and Android connection refuses sometimes (Not all times) which I tried but the problem was not solved. I had added Thread.sleep(1000) but that did not solve the problem but made the app slower so increasing the time did not seem worthwhile as it would have made the app slower.
The problem actually started recently when the server was migrated. It wasn't there earlier.
This is the code that I am using to access the server -
public String makeServiceCall(String url, int method,
List<NameValuePair> params) {
// try {
// Thread.sleep(1000);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
try {
// http client
// DefaultHttpClient httpClient = new MyHttpClient(context);//live
DefaultHttpClient httpClient = MyHttpClient.getInstance(context);//live
// DefaultHttpClient httpClient = new DefaultHttpClient();//buddy4
HttpEntity httpEntity = null;
HttpResponse httpResponse = null;
// Checking http request method type
if (method == POST) {
HttpPost httpPost = new HttpPost(url);
// adding post params
if (params != null) {
httpPost.setEntity(new UrlEncodedFormEntity(params));
}
httpResponse = httpClient.execute(httpPost);
} else if (method == GET) {
// appending params to url
if (params != null) {
String paramString = URLEncodedUtils
.format(params, "utf-8");
url += "?" + paramString;
}
HttpGet httpGet = new HttpGet(url);
httpResponse = httpClient.execute(httpGet);
}
httpEntity = httpResponse.getEntity();
response = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException | ClientProtocolException e) {
showErrorDialog(e.getMessage());
e.printStackTrace();
} catch (IOException e) {
showErrorDialog(e.getMessage());
e.printStackTrace();
}
return response;
}
And this is the code for MyHttpClient -
public class MyHttpClient extends DefaultHttpClient {
private static MyHttpClient instance = null;
public static MyHttpClient getInstance(Context context) {
if (instance == null) {
instance = new MyHttpClient(context);
}
return instance;
}
final Context context;
public MyHttpClient(Context context) {
this.context = context;
}
@Override
protected ClientConnectionManager createClientConnectionManager() {
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
// Register for port 443 our SSLSocketFactory with our keystore
// to the ConnectionManager
registry.register(new Scheme("https", newSslSocketFactory(), 443));
return new SingleClientConnManager(getParams(), registry);
}
private SSLSocketFactory newSslSocketFactory() {
try {
// Get an instance of the Bouncy Castle KeyStore format
KeyStore trusted = KeyStore.getInstance("BKS");
// Get the raw resource, which contains the keystore with
// your trusted certificates (root and any intermediate certs)
InputStream in = context.getResources().openRawResource(R.raw.mykeystore);
try {
// Initialize the keystore with the provided trusted certificates
// Also provide the password of the keystore
trusted.load(in, "mysecret".toCharArray());
} finally {
in.close();
}
// Pass the keystore to the SSLSocketFactory. The factory is responsible
// for the verification of the server certificate.
SSLSocketFactory sf = new SSLSocketFactory(trusted);
// Hostname verification from certificate
// http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html#d4e506
sf.setHostnameVerifier(SSLSocketFactory.STRICT_HOSTNAME_VERIFIER);
return sf;
} catch (Exception e) {
throw new AssertionError(e);
}
}
}
As you will notice I have commented out the Thread.sleep(1000) which I had tried but did not work.
Any help is appreciated. Thanks in advance.