之前可能已经提出过这个问题,但是在新版本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
的回复,这意味着我的身体参数不正确。
当我尝试使用其他工具作为邮递员提出请求时,我得到了正确的结果,代码是200。
我找到了this answer,但是Retrofit 2.0
我找不到TypedString类。
答案 0 :(得分:9)
<强>更新强>
Retrofit2.0现在拥有String
模块,用于com.squareup.retrofit2:converter-scalars
和原语(以及它们的盒装)。
Converter
您可以编写自定义String
,并且Retrofit存储库具有自己的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;
}
}
转换器实现示例:converter-scalars
var width = 400;
string jqmsg = " Dear Applicant,</br> <p>Your enquiry registered, The Email verification link has been sent to you registred Email ID " + txtEmail.Text + " and your registration reference ID is <Strong> " + Session["REGID"].ToString() + " </Strong></p>";
ClientScript.RegisterStartupScript(this.GetType(),"Popup", "ShowPopup('" + jqmsg + "', " + width + ");", true);
及相关问题已跟踪ToStringConverterFactory。
答案 1 :(得分:0)
基本上您需要编写自定义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
添加此内容答案 2 :(得分:0)
也许我错了,但你错过了一个“/”
@POST("token") ---> @POST("/token")