在android(间歇性)

时间:2015-07-03 07:27:19

标签: android apache http

我正在构建一个连接到Web服务器并以JSON格式提取数据的应用程序。我们正面临一个间歇性的问题,即连接被拒绝偶尔会发生一次。我跟着 - Android HTTP Connection refused和其他一些似乎并不相关的人和Android connection refuses sometimes (Not all times)我试过但问题没有解决。我添加了Thread.sleep(1000),但这并没有解决问题,但是使应用程序变慢,因此增加时间似乎不值得,因为它会使应用程序变慢。

最近,当迁移服务器时,问题就开始了。它早就没有了。

这是我用来访问服务器的代码 -

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;

    }

这是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);
        }
    }
}

正如您将注意到我已经注释掉了Thread.sleep(1000),我尝试过这种方法但是没有用。

任何帮助表示赞赏。提前谢谢。

0 个答案:

没有答案