我有一个像这样的改造对象:
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
和
@Headers
({
"Content-Type: application/x-www-form-urlencoded",
"cache-control: no-cache"
})
@POST("login")
Call<RegisterResponse> register(@Body String n1,@Body String n2);
我知道这是不正确的,因为有两个正文注释。
所以我必须使用这个代码
@Headers
({
"Content-Type: application/x-www-form-urlencoded",
"cache-control: no-cache"
})
@POST("login")
Call<RegisterResponse> register(@Body TestObject testObject);
class TestObject{
String n1;
String n2;
}
但我有一个服务器,我无法更改它,它有两个参数作为主体。
当我使用邮递员时,我的服务器工作得很好并且做它应该做的事情。
但是当我使用改造时,我得到了一个错误 500“服务器内部错误”
我已经用 okhttp 完成了这个
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
RequestBody body = RequestBody.create(mediaType, "n1=09369&n2=145616");
Request request = new Request.Builder()
.url(URL)
.post(body)
.addHeader("content-type", "application/x-www-form-urlencoded")
.addHeader("cache-control", "no-cache")
.addHeader("postman-token", "0761ac45-1fb7-78df-d088-2437ecb984a3")
.build();
okhttp3.Response response = client.newCall(request).execute();
并且它工作正常,但我如何通过改造来做到这一点?
你需要的是与@FormUrlEncoded注释发送数据,阅读更多关于它在这里
你可以这样使用它:
@FormUrlEncoded
@POST("login")
Call<RegisterResponse> register(@Field("n1") String n1, @Field("n2") String n2);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句