我正在使用上一个版本retrofit
连接到网络服务,但请告诉我以下错误:
okhttp3.internal.http2.StreamResetException: stream was reset: PROTOCOL_ERROR
对于巨大的okhttp3.internal.http2.StreamResetException: stream was reset: CANCEL
,这个错误是string
吗?或者**nginx**
服务器?
我的代码如下:
try {
// Create a trust manager that does not validate certificate chains
final TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
@Override
public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType)
throws CertificateException {}
@Override
public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType)
throws CertificateException {}
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new java.security.cert.X509Certificate[]{};
}
}
};
// Install the all-trusting trust manager
final SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
// Create an ssl socket factory with our all-trusting manager
final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.sslSocketFactory(sslSocketFactory, (X509TrustManager) trustAllCerts[0]);
builder.hostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
OkHttpClient okHttpClient = builder.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://xxx")
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.addConverterFactory(ScalarsConverterFactory.create())
.build();
final PublicApi request = retrofit.create(PublicApi.class);
Call<GetStatusSaveContactListModel> call = request.sendContactLists("saveContactList", obj.toString());
call.enqueue(new Callback<GetStatusSaveContactListModel>() {
@Override
public void onResponse(@NonNull Call<GetStatusSaveContactListModel> call, @NonNull
Response<GetStatusSaveContactListModel> response) {}
@Override
public void onFailure(@NonNull Call<GetStatusSaveContactListModel> call, Throwable t) {}
});
} catch (Exception e) {
throw new RuntimeException(e);
}
并且:
@POST("/web_service/mobile/rest")
Call<GetStatusSaveContactListModel> sendContactLists(@Query("function") String function,
@Query("data") String data);
并且:
implementation 'com.google.code.gson:gson:2.8.2'
implementation 'com.squareup.okhttp3:okhttp:3.10.0'
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.10.0'
implementation 'com.squareup.retrofit2:converter-scalars:2.3.0'
我正在向服务器发送一个巨大的 json
字符串:
{
"userData": {
"userId": "",
"s_code": "24557878"
},
"contactList": [{
"fname": "fffname",
"lname": "llname",
"name_prefix": "d",
"middle_name": "a",
"name_suffix": "z",
"mobile": "09232446557",
"emails": "sm.qa@yhgj.com"
}, {
"fname": "1fffname",
"lname": "1llname",
"name_prefix": "1d",
"middle_name": "1a",
"name_suffix": "1z",
"mobile": "109232446557",
"emails": ""
},
...............]
}
答案 0 :(得分:0)
我没有 Retrofit 2。+ 的经验,但在 Retrofit 1.9 中将json
作为String
发送到服务器{ {1}}请求我被迫在POST
TypedJsonString
并使用此接口定义:
import retrofit.mime.TypedString;
public class TypedJsonString extends TypedString {
public TypedJsonString(String body) {
super(body);
}
@Override public String mimeType() {
return "application/json";
}
}
从后台线程发出请求:
@POST(BASE_METHOD)
Response postProductRequest(@Body TypedJsonString productRequest);