问题描述
将 OkHttpClient 对象添加到改造中时出现错误.
I am getting error while adding OkHttpClient object into retrofit.
错误是:com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:已关闭
Error is: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: closed
这是我的代码:
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文件.
I have include below gradle files.
- 编译'com.squareup.retrofit:retrofit:2.0.0-beta2'
- 编译'com.squareup.retrofit:converter-gson:2.0.0-beta2'
- 编译'com.squareup.okhttp:okhttp:2.7.0'
使用上述所有代码,我得到的错误是关于失败:com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:已关闭
With All above code i am getting error which is on Failure: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: closed
我尝试了如下所示的组合.
i have tried some combination like below.
如果我只是使用 OkHttpClient okhttpclient = new OkHttpClient()而不是 getUnsafeOkHttpClient()
if i simply used OkHttpClient okhttpclient = new OkHttpClient() instead of getUnsafeOkHttpClient()
并添加此okhttpclient对象以进行改进,例如 .client(okhttpclient),而不是 .client(getUnsafeOkHttpClient()),但我没有得到任何错误.它将执行 onResponse()
And add this okhttpclient object to retrofit like .client(okhttpclient) instead of .client(getUnsafeOkHttpClient()) i do not get any error. it will execute onResponse()
出于安全目的,我必须使用 getUnsafeOkHttpClient().谁能告诉我我在哪里做错了?
I have to use getUnsafeOkHttpClient() for security purpose. can anyone tell me where i am doing wrong?
推荐答案
请注意,Retrofit 2依赖于OkHttp进行网络操作,因此,一旦添加了Retrofit2,您就无需显式添加OkHttp依赖项.我认为这可能会导致版本冲突.
Note that Retrofit 2 relies on OkHttp for its network operations Hence you will not need to add OkHttp dependency explicitly once you have added the retrofit2. I believe that could be causing the version conflict.
编辑:将beta 3用于翻新和gson
Edit: Use beta 3 for both retrofit and gson
compile 'com.squareup.retrofit2:retrofit:2.0.0-beta3'
compile 'com.squareup.retrofit2:retrofit-converters:2.0.0-beta3'
compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta3
这篇关于使用Retrofit在onFailure上获取错误:com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:已关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!