我在Asynchttpclient
的https上遇到问题,它没有显示任何错误,但是响应在android中为空。
这是我在调用网络服务时使用的代码:
public static void callWebService(final MyActivity myActivity, final MyFragment myFragment, final RequestParams params, final String url, final String tag, final boolean isCallOnFailure) {
ConnectivityManager connectivityManager = (ConnectivityManager) myActivity.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
if (activeNetworkInfo != null && activeNetworkInfo.isConnected()) {
final ProgressDialog prgDialog = ProgressDialog.show(myActivity, null,null);
ProgressBar spinner = new ProgressBar(myActivity, null,android.R.attr.progressBarStyle);
spinner.getIndeterminateDrawable().setColorFilter(Color.parseColor("#53CBF1"), android.graphics.PorterDuff.Mode.SRC_IN);
prgDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
prgDialog.setContentView(spinner);
prgDialog.setIndeterminate(true);
prgDialog.setCancelable(tag.equals("signup")?false:true);
prgDialog.show();
Log.i("url", url);
AsyncHttpClient newclient = new AsyncHttpClient();
newclient.setTimeout(40000);
newclient.post(url,params,new AsyncHttpResponseHandler() {
@Override //onSucess
public void onSuccess(String response) {
if(prgDialog != null && prgDialog.isShowing())
prgDialog.dismiss();
if (myFragment != null)
myFragment.onSuccess(response,tag);
else
myActivity.onSuccess(response,tag);
}
@Override //onFailure
public void onFailure(int statusCode, Throwable error, String content) {
Toast.makeText(myActivity, "Network Problem or Something went wrong at server end !!", Toast.LENGTH_SHORT).show();
if(prgDialog != null && prgDialog.isShowing())
prgDialog.dismiss();
if (isCallOnFailure) {
if (myFragment != null)
myFragment.onFailure();
else
myActivity.onFailure(tag);
}
}
});
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
// Hide Progress Dialog
if (prgDialog != null && prgDialog.isShowing()) {
Toast.makeText(myActivity, "Connection Terminated.", Toast.LENGTH_LONG).show();
prgDialog.dismiss();
}
}
},40000);
}
else
Toast.makeText(myActivity, "Please check your Network connection..!!!", Toast.LENGTH_LONG).show();
}
在请求api时,我编写了这段代码:
RequestParams params = new RequestParams();params.put("username",tlLoginUserNm.getEditText().getText().tString());
params.put("password", tlLoginPassword.getEditText().getText().toString());
common.callWebService(LoginActivity.this, null, params,wbPath+"login.php", "login", false);
答案 0 :(得分:1)
是的,确实有效,
为SSLSocketFactory
创建一个类,如下所示:
public class MySSLSocketFactory extends SSLSocketFactory {
SSLContext sslContext = SSLContext.getInstance("TLS");
public MySSLSocketFactory(KeyStore truststore) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException {
super(truststore);
TrustManager tm = new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
sslContext.init(null, new TrustManager[]{tm}, null);
}
@Override
public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException, UnknownHostException {
return sslContext.getSocketFactory().createSocket(socket, host, port, autoClose);
}
@Override
public Socket createSocket() throws IOException {
return sslContext.getSocketFactory().createSocket();
}
}
,并在使用异步任务创建api的方法请求api时添加http客户端,如下所示:
public HttpClient getNewHttpClient() {
try {
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
trustStore.load(null, null);
SSLSocketFactory sf = new SSLSocketFactory(trustStore);
sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
registry.register(new Scheme("https", new MySSLSocketFactory(trustStore), 443));
ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);
return new DefaultHttpClient(ccm, params);
} catch (Exception e) {
return new DefaultHttpClient();
}
}
最后在您的doinbackground中获得您的Http Client,如下所示:
HttpClient client = getNewHttpClient();