Android Volley https SSL自签名和Google Maps API

Android Volley https SSL自签名和Google Maps API

问题描述:

我们正在使用Android Volley并使用可正常运行的自证书SSL,但现在我们要实施Google Maps并且它不起作用;它只是不抛出任何错误,它只是显示一个灰色的屏幕Android Volley https SSL自签名和Google Maps API

enter image description here 这是我们实现抽射:

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:

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创建的通用平台:

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

及其位置:

<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

我们如何才能实现Map活动?谢谢!

+1

请在地图上显示代码并设置其位置。 – Barns

+1

日志中是否有任何错误消息?也许关于API密钥和认证的东西? – xomena

+0

@ xomena是,“无法加载地图,联系Google服务器时出错,这可能是一个身份验证问题”,但是:1)我不是第一次输入地图,而是第二次输入地图。和2)只有在使用过SSL工厂之后;也就是说,如果我直接访问应用程序到地图,它完美的作品 – Ramon

尝试添加了谷歌地图的客户打电话到的HostnameVerifier:

回报hostname.compareTo( “OUR_SERVER_HOSTNAME”)== 0 || hostname.compareTo(“clients4.google.com”)== 0;

由于某种原因,一旦使用了HostnameVerfiier类,API调用就会被代码捕获。

+0

谢谢,你救了我的月! – Ramon