之前可能已经提出过这个问题,但是对于新版本2.0,我还没有找到任何正确的答案.
我的问题如下:
public interface AuthenticationAPI {
@POST("token")
Call<String> authenticate(@Body String body);
}
Run Code Online (Sandbox Code Playgroud)
然后我打电话给:
Retrofit retrofit = new Retrofit.Builder().baseUrl(authenUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();
AuthenticationAPI authen = retrofit.create(AuthenticationAPI.class);
try {
Call<String> call1 = authen.authenticate(authenBody);
call1.enqueue(new Callback<String>() {
@Override
public void onResponse(Response<String> response, Retrofit retrofit) {
Log.d("THAIPD", "Success " + response.raw().message());
}
@Override
public void onFailure(Throwable t) {
Log.e("THAIPD", " FAIL " + t.getMessage());
}
});
} catch (Exception e) {
Log.e("THAIPD", e.getMessage());
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
然后我收到响应protocol=http/1.1, code=400, message=Bad Request,这意味着我的身体参数不正确.
当我尝试使用其他工具作为Postman提出请求时,我得到了正确的结果,代码是200.
我找到了这个答案,但Retrofit 2.0我找不到TypedString类.
ytR*_*ino 10
更新:
Retrofit2.0现在拥有自己的转换器 - 标量模块String和基元(及其盒装).
com.squareup.retrofit2:converter-scalars
Run Code Online (Sandbox Code Playgroud)
您可以编写自定义Converter和Retrofit存储库有一个自己的StringConverter实现示例:ToStringConverterFactory
class ToStringConverterFactory extends Converter.Factory {
private static final MediaType MEDIA_TYPE = MediaType.parse("text/plain");
@Override
public Converter<ResponseBody, ?> fromResponseBody(Type type, Annotation[] annotations) {
if (String.class.equals(type)) {
return new Converter<ResponseBody, String>() {
@Override public String convert(ResponseBody value) throws IOException {
return value.string();
}
};
}
return null;
}
@Override public Converter<?, RequestBody> toRequestBody(Type type, Annotation[] annotations) {
if (String.class.equals(type)) {
return new Converter<String, RequestBody>() {
@Override public RequestBody convert(String value) throws IOException {
return RequestBody.create(MEDIA_TYPE, value);
}
};
}
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
这里跟踪相关问题.
| 归档时间: |
|
| 查看次数: |
7925 次 |
| 最近记录: |