Android Volley https SSL自签名和Google Maps API [英] Android Volley Https SSL self signed and Google Maps API

查看:187
本文介绍了Android Volley https SSL自签名和Google Maps API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在与Android Volley合作,并使用可正常工作的自我认证SSL,但现在我们希望实施Google地图并且无法使用;它只是不会抛出任何错误,它只是显示一个灰色的屏幕

We are working with Android Volley and using a self certificated SSL that works correctly, but now we want to implement Google Maps and it doesn't work; it just doesn't throws any error, it just shows a greyed out screen


这是我们的Volley的实现:

This is our implementation of Volley:

public class AppSingleton {
private static AppSingleton mAppSingletonInstance;
private RequestQueue mRequestQueue;
private static Context mContext;

private AppSingleton(Context context) {
    mContext = context;
    mRequestQueue = getRequestQueue();
}

public static synchronized AppSingleton getInstance(Context context) {
    if (mAppSingletonInstance == null) {
        mAppSingletonInstance = new AppSingleton(context);
    }
    return mAppSingletonInstance;
}

private RequestQueue getRequestQueue() {
    if (mRequestQueue == null) {
        mRequestQueue = Volley.newRequestQueue(mContext.getApplicationContext(), new HurlStack(null, getSocketFactory()));
    }
    return mRequestQueue;
}

public <T> void addToRequestQueue(Request<T> req, String tag) {
    req.setTag(tag);
    getRequestQueue().add(req);
}

这就是我们的getSocketFactory:

And this is our getSocketFactory:

private SSLSocketFactory getSocketFactory() {

CertificateFactory cf = null;
try {
    cf = CertificateFactory.getInstance("X.509");
    InputStream caInput = mContext.getResources().openRawResource(OUR_CERT);
    Certificate ca;
    try {
        ca = cf.generateCertificate(caInput);
        Log.e("CERT", "ca=" + ((X509Certificate) ca).getSubjectDN());
    } finally {
        caInput.close();
    }


    String keyStoreType = KeyStore.getDefaultType();
    KeyStore keyStore = KeyStore.getInstance(keyStoreType);
    keyStore.load(null, null);
    keyStore.setCertificateEntry("ca", ca);


    String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
    TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
    tmf.init(keyStore);


    HostnameVerifier hostnameVerifier = new HostnameVerifier() {
        @Override
        public boolean verify(String hostname, SSLSession session) {

            Log.e("CipherUsed", session.getCipherSuite());
            return hostname.compareTo("OUR_SERVER_HOSTNAME")==0;

        }
    };


    HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
    SSLContext context = null;
    context = SSLContext.getInstance("TLS");

    context.init(null, tmf.getTrustManagers(), null);
    HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());

    SSLSocketFactory sf = context.getSocketFactory();


    return sf;

} catch (CertificateException | NoSuchAlgorithmException | KeyStoreException | IOException | KeyManagementException e) {
    e.printStackTrace();
}

return  null; }

MapActivity是Android Studio创建的通用类型:

The MapActivity is the generic one created by Android Studio:

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps2);
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    // Add a marker in Sydney and move the camera
    LatLng sydney = new LatLng(-34, 151);
    mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}}

及其位置:

And its location:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="cat.amb.parcandride.MapsActivity" />

在activity_maps.xml中

In a activity_maps.xml

我们能否实施地图活动?谢谢!

How could we implement the Map Activity? Thank you!

推荐答案

尝试将Google地图客户端调用添加到hostnameVerifier:

Try to add the google map clients call to the hostnameVerifier:

返回hostname.compareTo(OUR_SERVER_HOSTNAME)== 0 || hostname.compareTo(clients4.google.com)== 0;

return hostname.compareTo("OUR_SERVER_HOSTNAME")==0 || hostname.compareTo("clients4.google.com") == 0;

由于某些原因,API调用会被您的代码一旦使用HostnameVerfiier类。

for some reason the call to the API gets caught by your code once the HostnameVerfiier class is used.

这篇关于Android Volley https SSL自签名和Google Maps API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆