0
我在使用HttpsUrlConnection對象獲取Android應用程序連接到https服務器時遇到了問題。任何人都可以幫助我解決這個問題。如何解決Android中的volley.NoConnectionError?
我正在創建一個像這樣的BKS文件。
keytool -importcert -v -trustcacerts -file "C:\path_to_file\app_cert.crt" -alias lrl -keystore "keystore.bks" -provider org.bouncycastle.jce.provider.BouncyCastleProvider -providerpath "C:\Program Files (x86)\Java\jre7\bin\bcprov-ext-jdk15on-1.46.jar" -storetype BKS -storepass "password"
在我的主文件:
public class Application extends android.app.Application {
private static Application mInstance;
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private static char[] KEYSTORE_PASSWORD = "password".toCharArray();
private PreferenceManager pref;
public static final String TAG = Application.class.getSimpleName();
@Override
public void onCreate() {
mInstance = this;
super.onCreate();
}
public static synchronized Application getInstance() {
return mInstance;
}
@Override
public void onTerminate() {
FontUtils.clean();
super.onTerminate();
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
//mRequestQueue = Volley.newRequestQueue(getApplicationContext());
mRequestQueue = Volley.newRequestQueue(getApplicationContext(), new HurlStack(null, newSslSocketFactory()));
mRequestQueue.start();
}
return mRequestQueue;
}
public ImageLoader getImageLoader() {
getRequestQueue();
if (mImageLoader == null) {
mImageLoader = new ImageLoader(this.mRequestQueue,
new LruBitmapCache());
}
return this.mImageLoader;
}
public PreferenceManager getPrefManager() {
if (pref == null) {
pref = new PreferenceManager(this);
}
return pref;
}
public <T> void addToRequestQueue(Request<T> req, String tag) {
// set the default tag if tag is empty
req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
getRequestQueue().add(req);
}
public <T> void addToRequestQueue(Request<T> req) {
req.setTag(TAG);
getRequestQueue().add(req);
}
public void cancelPendingRequests(Object tag) {
if (mRequestQueue != null) {
mRequestQueue.cancelAll(tag);
}
}
private static SSLSocketFactory newSslSocketFactory() {
try {
KeyStore trusted = KeyStore.getInstance("BKS"); //PKCS7
InputStream in = Application.getInstance().getResources().openRawResource(R.raw.keystore);
try {
trusted.load(in, KEYSTORE_PASSWORD);
} finally {
in.close();
}
String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
tmf.init(trusted);
SSLContext context = SSLContext.getInstance("TLS");
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String arg0, SSLSession arg1) {
Log.e(TAG, "ARG =" + arg0 + " == arg1" + arg1);
return true;
}
});
context.init(null, tmf.getTrustManagers(), null);
SSLSocketFactory sf = context.getSocketFactory();
return sf;
} catch (Exception e) {
throw new AssertionError(e);
}
}
}
我的網絡處理程序文件
public class NetworkRequestHandler {
private String TAG = NetworkRequestHandler.class.getSimpleName();
private static NetworkRequestHandler mInstance = null;
private ServerResponseListener mListener=null;
public static NetworkRequestHandler getInstance() {
if (mInstance == null) {
mInstance = new NetworkRequestHandler();
}
return mInstance;
}
public void setListener(ServerResponseListener listener)
{
mListener= listener;
}
public void sendNetworkRequest(String endPoint,final Map<String, String> params,final String responseType, int requestType) {
try {
String url= Config.BASE_URL+endPoint;
System.out.println("URL =="+url);
StringRequest strReq = new StringRequest(requestType,url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Logger.d("","response " + response);
if(mListener != null) {
mListener.success(response, responseType);
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
if(mListener != null) {
mListener.failure();
}
}
})
{
/**
* Passing some request headers
* */
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/x-www-form-urlencoded");
return headers;
}
@Override
public Map<String, String> getParams() {
return params;
}
};
strReq.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
Application.getInstance().addToRequestQueue(strReq, responseType);
} catch (Exception e) {
Logger.d(TAG, "Problem while doing HTTP Request");
if(mListener != null) {
mListener.failure();
}
}
}
我得到了以下異常
06-10 04:38:41.597 5936-5936/com.aetasapp W/System.err: com.android.volley.NoConnectionError: javax.net.ssl.SSLException: Connection closed by peer
06-10 04:38:41.605 5936-5936/com.aetasapp W/System.err: at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:151)
06-10 04:38:41.605 5936-5936/com.aetasapp W/System.err: at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)
06-10 04:38:41.605 5936-5936/com.aetasapp W/System.err: Caused by: javax.net.ssl.SSLException: Connection closed by peer
06-10 04:38:41.613 5936-5936/com.aetasapp W/System.err: at org.apache.harmony.xnet.provider.jsse.NativeCrypto.SSL_do_handshake(Native Method)
06-10 04:38:41.613 5936-5936/com.aetasapp W/System.err: at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:398)
06-10 04:38:41.613 5936-5936/com.aetasapp W/System.err: at libcore.net.http.HttpConnection.setupSecureSocket(HttpConnection.java:209)
06-10 04:38:41.613 5936-5936/com.aetasapp W/System.err: at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:478)
06-10 04:38:41.613 5936-5936/com.aetasapp W/System.err: at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:442)
06-10 04:38:41.613 5936-5936/com.aetasapp W/System.err: at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
06-10 04:38:41.620 5936-5936/com.aetasapp W/System.err: at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
06-10 04:38:41.620 5936-5936/com.aetasapp W/System.err: at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81)
06-10 04:38:41.620 5936-5936/com.aetasapp W/System.err: at libcore.net.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:197)
06-10 04:38:41.620 5936-5936/com.aetasapp W/System.err: at libcore.net.http.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:281)
06-10 04:38:41.620 5936-5936/com.aetasapp W/System.err: at com.android.volley.toolbox.HurlStack.addBodyIfExists(HurlStack.java:240)
06-10 04:38:41.620 5936-5936/com.aetasapp W/System.err: at com.android.volley.toolbox.HurlStack.setConnectionParametersForRequest(HurlStack.java:210)
06-10 04:38:41.620 5936-5936/com.aetasapp W/System.err: at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:106)
06-10 04:38:41.620 5936-5936/com.aetasapp W/System.err: at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:96)
06-10 04:38:41.620 5936-5936/com.aetasapp W/System.err: ... 1 more