将 OkHttpClient 对象添加到改造中时出错 .
Error is: com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:已关闭
这是我的 Code :
public static OkHttpClient getUnsafeOkHttpClient() {
try {
File mFolder = new File(Environment.getExternalStorageDirectory() + "/certificate");
if (!mFolder.exists())
{
mFolder.mkdir();
}
String fileName = "certificate1a.cer";
File file = new File(mFolder, fileName);
FileInputStream fis = null;
fis = new FileInputStream(file);
CertificateFactory cf = CertificateFactory.getInstance("X.509");
Certificate ca = cf.generateCertificate(fis);
// Create a KeyStore containing our trusted CAs
String keyStoreType = KeyStore.getDefaultType();
KeyStore keyStore = KeyStore.getInstance(keyStoreType);
keyStore.load(null, null);
keyStore.setCertificateEntry("ca", ca);
// Create a TrustManager that trusts the CAs in our KeyStore
String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
tmf.init(keyStore);
// Install the all-trusting trust manager
final SSLContext sslContext = SSLContext.getInstance("TSL");
sslContext.init(null, tmf.getTrustManagers(),null);
// Create an ssl socket factory with our all-trusting manager
final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.setSslSocketFactory(sslSocketFactory);
okHttpClient.interceptors().add(new APIRequestInterceptor());
okHttpClient.interceptors().add(new APIResponseInterceptor());
okHttpClient.setHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession sslSession) {
HostnameVerifier hv = HttpsURLConnection.getDefaultHostnameVerifier();
Log.e("hey", "inside this");
Log.e("HOST VERIFIER", hv.toString());
Log.e("HOST NAME", hostname);
return hv.verify("aviatesoftware.in", sslSession);
}
});
return okHttpClient;
} catch (Exception e) {
Log.e("error while getting ","unsafeOkHttpClient "+e.toString());
}
return null;
}
public static <S> S createService(Class<S> serviceClass) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(API_BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(getUnsafeOkHttpClient())
.build();
return retrofit.create(serviceClass);
}
GetInterface obj = createService(GetTaxInterface.class);
Call<Abc> call = obj.getAbc("555555555555");
call.enqueue(new Callback<Abc>() {
@Override
public void onResponse(Response<Abc> response, Retrofit retrofit) {
Log.e("Asynchronous response", response.toString());
Abc temp = response.body();
Log.e("tax object", temp.toString());
Toast.makeText(getApplicationContext(),"Retrofit Asynchonus Simple try successful",Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(Throwable t) {
Log.e("on Failure", t.toString());
Toast.makeText(getApplicationContext(),"Retrofit Asynchonus Simple try failed",Toast.LENGTH_SHORT).show();
}
});
我已经包含以下gradle文件 .
-
编译'com.squareup.retrofit:retrofit:2.0.0-beta2'
-
编译'com.squareup.retrofit:converter-gson:2.0.0-beta2'
-
编译'com.squareup.okhttp:okhttp:2.7.0'
使用以上所有代码我收到的错误是 on Failure: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: closed
我尝试过如下组合 .
如果我只是使用 OkHttpClient okhttpclient = new OkHttpClient() 而不是 getUnsafeOkHttpClient()
并添加这个okhttpclient对象改造像 .client(okhttpclient) 而不是 .client(getUnsafeOkHttpClient()) 我没有得到任何错误 . 它会执行 onResponse()
出于安全考虑,我必须使用 getUnsafeOkHttpClient() . 谁能告诉我我做错了什么?
1 回答
请注意,Retrofit 2依赖于OkHttp进行网络操作 . 因此,一旦添加了retrofit2,就不需要显式添加OkHttp依赖项 . 我相信这可能会导致版本冲突 .
Edit :使用beta 3进行改造和gson