gpt4 book ai didi

Android Volley Https SSL 自签名和 Google Maps API

转载 作者:太空狗 更新时间:2023-10-29 13:06:48 24 4
gpt4 key购买 nike

我们正在使用 Android Volley 并使用可以正常工作的自认证 SSL,但现在我们想要实现 Google map ,但它不起作用;它只是不会抛出任何错误,它只是显示一个灰色的屏幕

enter image description here这是我们对 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:

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 Activity ?谢谢!

最佳答案

尝试将 google map 客户端调用添加到 hostnameVerifier:

返回主机名.compareTo("OUR_SERVER_HOSTNAME")==0 || hostname.compareTo("clients4.google.com") == 0;

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

关于Android Volley Https SSL 自签名和 Google Maps API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46833983/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com