Connexion au serveur refusée sous Android (intermittente)

Je construis une application qui se connecte à un serveur Web et récupère les données en tant que JSON. Nous sums confrontés à un problème intermittent: la connexion refusée se produit de temps en temps pas toujours. J’ai suivi – Android HTTP Connection a refusé et quelques autres qui ne semblaient pas pertinents et la connexion Android refuse parfois (Pas tous les temps) que j’ai essayé mais le problème n’a pas été résolu. J’avais ajouté Thread.sleep (1000), mais cela n’a pas résolu le problème, mais le ralentissement de l’application n’a pas semblé intéressant, car cela aurait ralenti l’application.

Le problème a effectivement commencé récemment lors de la migration du serveur. Ce n’était pas là avant.

C’est le code que j’utilise pour accéder au serveur –

public Ssortingng makeServiceCall(Ssortingng url, int method, List 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) { Ssortingng paramSsortingng = URLEncodedUtils .format(params, "utf-8"); url += "?" + paramSsortingng; } HttpGet httpGet = new HttpGet(url); httpResponse = httpClient.execute(httpGet); } httpEntity = httpResponse.getEntity(); response = EntityUtils.toSsortingng(httpEntity); } catch (UnsupportedEncodingException | ClientProtocolException e) { showErrorDialog(e.getMessage()); e.printStackTrace(); } catch (IOException e) { showErrorDialog(e.getMessage()); e.printStackTrace(); } return response; } 

Et ceci est le code pour 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 certificatees (root and any intermediate certs) InputStream in = context.getResources().openRawResource(R.raw.mykeystore); try { // Initialize the keystore with the provided trusted certificatees // 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 certificatee. SSLSocketFactory sf = new SSLSocketFactory(trusted); // Hostname verification from certificatee // 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); } } } 

Comme vous le remarquerez, j’ai commenté le Thread.sleep (1000) que j’avais essayé mais que je n’ai pas fonctionné.

Toute aide est appréciée. Merci d’avance.