问题描述
我正在使用最新版本retrofit
连接到Web服务,但出现以下错误:
I am connecting to web service with last version retrofit
but get me bellow error :
okhttp3.internal.http2.StreamResetException: stream was reset: PROTOCOL_ERROR
此错误okhttp3.internal.http2.StreamResetException: stream was reset: CANCEL
是否是大型string
的错误?还是服务器的**nginx**
?
Is this error okhttp3.internal.http2.StreamResetException: stream was reset: CANCEL
for huge string
? or for **nginx**
of server ?
我的代码就像下面这样:
My code is like bellow :
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
作为字符串发送到服务器:
I am sending a huge json
as string to server :
{
"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": ""
},
...............]
}
推荐答案
我没有使用 Retrofit 2.+ 的经验,但是在 Retrofit 1.9 中发送json
以POST
请求中的String
的身份发送到服务器,我被迫将其封装在TypedJsonString
I have no experience with Retrofit 2.+, but in Retrofit 1.9 to send json
to server as a String
in a POST
request I have been forced to envelop it in 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);
从后台线程发出请求:
String requestBody = new Gson().toJson(new ProductRequest());
retrofit.client.Response r = mApiService.postProductRequest(new TypedJsonString(requestBody));
这篇关于将Json作为字符串发送到服务器(nginx)-改造2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!