body 的 Retrofit 2.0 POST 方法是字符串
Retrofit 2.0 POST method with body is String
这个问题以前可能有人问过,但在新版本 2.0 中我还没有找到任何正确答案。
我的问题如下:
public interface AuthenticationAPI {
@POST("token")
Call<String> authenticate(@Body String body);
}
然后我打电话给:
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();
}
然后我收到 protocol=http/1.1, code=400, message=Bad Request
的响应,这意味着我的 body 参数不正确。
当我尝试通过其他工具作为 Postman 发出请求时,我得到正确的结果,代码是 200。
我找到了 this answer 但是 Retrofit 2.0
我找不到 TypedString class.
这是我用其他工具(DHC)尝试的结果
基本上你需要写一个自定义的Converter
像这样
public final class StringConverterFactory extends Converter.Factory {
public static StringConverterFactory create(){
return new StringConverterFactory();
}
@Override
public Converter<ResponseBody, ?> fromResponseBody(Type type, Annotation[] annotations) {
return new ConfigurationServiceConverter();
}
final class ConfigurationServiceConverter implements Converter<ResponseBody, String>{
@Override
public String convert(ResponseBody value) throws IOException {
BufferedReader r = new BufferedReader(new InputStreamReader(value.byteStream()));
StringBuilder total = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
total.append(line);
}
return total.toString();
}
}
}
并使用 addConverterFactory
添加
更新:
Retrofit2.0 现在拥有用于 String
和基元(及其盒装)的 converter-scalars 模块。
com.squareup.retrofit2:converter-scalars
您可以编写自定义 Converter
并且 Retrofit 存储库有自己的 String
转换器实现示例: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;
}
}
相关问题已跟踪 here。
也许我错了,但是你少了一个“/”
@POST("token") ---> @POST("/token")
这个问题以前可能有人问过,但在新版本 2.0 中我还没有找到任何正确答案。
我的问题如下:
public interface AuthenticationAPI {
@POST("token")
Call<String> authenticate(@Body String body);
}
然后我打电话给:
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();
}
然后我收到 protocol=http/1.1, code=400, message=Bad Request
的响应,这意味着我的 body 参数不正确。
当我尝试通过其他工具作为 Postman 发出请求时,我得到正确的结果,代码是 200。
我找到了 this answer 但是 Retrofit 2.0
我找不到 TypedString class.
这是我用其他工具(DHC)尝试的结果
基本上你需要写一个自定义的Converter
像这样
public final class StringConverterFactory extends Converter.Factory {
public static StringConverterFactory create(){
return new StringConverterFactory();
}
@Override
public Converter<ResponseBody, ?> fromResponseBody(Type type, Annotation[] annotations) {
return new ConfigurationServiceConverter();
}
final class ConfigurationServiceConverter implements Converter<ResponseBody, String>{
@Override
public String convert(ResponseBody value) throws IOException {
BufferedReader r = new BufferedReader(new InputStreamReader(value.byteStream()));
StringBuilder total = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
total.append(line);
}
return total.toString();
}
}
}
并使用 addConverterFactory
添加更新:
Retrofit2.0 现在拥有用于 String
和基元(及其盒装)的 converter-scalars 模块。
com.squareup.retrofit2:converter-scalars
您可以编写自定义 Converter
并且 Retrofit 存储库有自己的 String
转换器实现示例: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;
}
}
相关问题已跟踪 here。
也许我错了,但是你少了一个“/”
@POST("token") ---> @POST("/token")