2017-10-19 8 views
3

Google은 Android Volley와 협력하고 올바르게 작동하는 자체 인증 SSL을 사용하지만 이제 Google지도를 구현하려고하지만 작동하지 않습니다. 이 오류가 발생합니다뿐만 아니라 않는, 그냥 회색 화면Android Volley Https SSL 자체 서명 및 Google지도 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)); 
}} 

그리고 그 위치하십시오 activity_maps.xml

우리는 어떻게지도 활동을 구현할 수에서

<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" /> 

? 고맙습니다!

반환 hostname.compareTo ("OUR_SERVER_HOSTNAME") == 0 || :

+1

지도를 구현하고 위치를 설정하는 코드를 표시하십시오. – Barns

+1

로그에 오류 메시지가 있습니까? 어쩌면 API 키와 인증에 관한 것입니까? – xomena

+0

@xomena 예, "지도를로드하지 못했습니다 .Google 서버에 연결하는 중 오류가 발생했습니다. 아마도 인증 문제 일 것입니다."그러나 다음을 수행해야합니다. 2) SSL 공장을 사용한 후에 만; 즉,지도에 직접 앱에 액세스하면 완벽하게 작동합니다. – Ramon

답변

1

시도 클라이언트가 설정되어있는 HostnameVerifier에 전화를 구글 맵을 추가하는 hostname.compareTo ("clients4.google.com") == 0;

HostnameVerfiier 클래스가 사용되면 API 호출이 귀하의 코드에 의해 캐치됩니다.

+0

감사합니다. 월을 저장하십시오! – Ramon