Java 如何在Android中使用改型2获得Post请求抛出的纯文本错误消息?
因此,我向服务器发出了这个POST请求,并且基于一个参数,服务器将在改型的errorBody()中返回错误消息。我试图处理服务器返回的纯文本错误,然后在使用Java的Android应用程序中向用户显示它。以下是我当前的尝试,但这给了我Logcat中的错误:Java 如何在Android中使用改型2获得Post请求抛出的纯文本错误消息?,java,android,api,post,retrofit,Java,Android,Api,Post,Retrofit,因此,我向服务器发出了这个POST请求,并且基于一个参数,服务器将在改型的errorBody()中返回错误消息。我试图处理服务器返回的纯文本错误,然后在使用Java的Android应用程序中向用户显示它。以下是我当前的尝试,但这给了我Logcat中的错误: @Url cannot be used with @POST URL (parameter #1) 以下是来自服务器的400响应: 接口: public interface ChangePickLocationClient { @GET
@Url cannot be used with @POST URL (parameter #1)
以下是来自服务器的400响应:
接口:
public interface ChangePickLocationClient
{
@GET
Call<ResponseBody> checkItem(@Url String url, @Header("Authorization") String authToken);
@GET
Call<String> getStringError(@Url String url, @Header("Authorization") String authToken);
@POST("Pick/ChangePickLocationAcceptChange")
Call<String> changePickLocationPOST(@Url String url, @Header("Authorization") String authToken, @Body
ChangePickLocationPostModel changePickLocationPostModel);
}
公共接口ChangePickLocationClient
{
@得到
调用checkItem(@Url字符串Url,@Header(“授权”)字符串authToken);
@得到
调用getStringError(@Url字符串Url,@Header(“授权”)字符串authToken);
@POST(“拾取/更改PickLocationAcceptChange”)
调用changePickLocationPOST(@Url字符串Url,@Header(“授权”)字符串authToken,@Body
ChangePickLocationPostModel ChangePickLocationPostModel);
}
实施:
private static final String BASE_URL = "http://00.00.00.1234/api/";
Gson mGson = new Gson();
Retrofit retrofit = new Retrofit.Builder().client(new OkHttpClient())
.baseUrl(BASE_URL).addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create(mGson))
.build();
ChangePickLocationClient ChangePickLocationClient =
retrofitPOST.create(ChangePickLocationClient.class);
String itemNumber = itemNumberValue.getText().toString();
newPickLocationValue.setText(newPickLocationValue.getText().toString().toUpperCase());
String newPickLocation = newPickLocationValue.getText().toString();
String token = globalClass.getActiveToken();
final ChangePickLocationClient mChangePickLocationInterface =
retrofit.create(ChangePickLocationClient.class);
Call<String> mCallErrorPOST = mChangePickLocationInterface.changePickLocationPOST
(postUrl, "Bearer " + globalClass.getActiveToken(),
changePickLocationPostModel);
call.enqueue(new Callback<ChangePickLocationPostModel>()
{
@Override
public void onResponse(Call<ChangePickLocationPostModel> call,
Response<ChangePickLocationPostModel> response)
{
String mPlainTextResponse = null;
try {
if(response.errorBody() != null)
{
mPlainTextResponse = response.errorBody().string();
}
} catch (IOException e)
{
e.printStackTrace();
}
Toast.makeText(ChangePickLocation.this, mPlainTextResponse
,Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(Call<ChangePickLocationPostModel> call, Throwable t)
{
Toast.makeText(ChangePickLocation.this, "Unknown server error!"
,Toast.LENGTH_SHORT).show();
}
});
private静态最终字符串BASE\u URL=”http://00.00.00.1234/api/";
Gson mGson=新的Gson();
改装改装=新改装.Builder().client(新OkHttpClient())
.baseUrl(BASE_URL).addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create(mGson))
.build();
ChangePickLocationClient ChangePickLocationClient=
创建(ChangePickLocationClient.class);
String itemNumber=itemNumberValue.getText().toString();
newPickLocationValue.setText(newPickLocationValue.getText().toString().toUpperCase());
字符串newPickLocation=newPickLocationValue.getText().toString();
字符串标记=globalClass.getActiveToken();
最终更改PickLocationClient mChangePickLocationInterface=
创建(ChangePickLocationClient.class);
调用mCallErrorPOST=mChangePickLocationInterface.changePickLocationPOST
(postrl,“Bearer”+globalClass.getActiveToken(),
changePickLocationPostModel);
call.enqueue(新回调()
{
@凌驾
公共void onResponse(调用,
回应(回应)
{
字符串mPlainTextResponse=null;
试一试{
if(response.errorBody()!=null)
{
mPlainTextResponse=response.errorBody().string();
}
}捕获(IOE异常)
{
e、 printStackTrace();
}
Toast.makeText(ChangePickLocation.this,mplaintExtraponse
,Toast.LENGTH_SHORT)show();
}
@凌驾
失败时公共无效(调用调用,可丢弃的t)
{
Toast.makeText(ChangePickLocation.this,“未知服务器错误!”
,Toast.LENGTH_SHORT)show();
}
});
当响应为400时,第二个调用需要是clone()调用。这是因为如文档中所述,调用不能被多次使用
使用以下命令:
call.clone().enqueue(new Callback<ChangePickLocationPostModel>()
call.clone().enqueue(新回调()
而不是
call.enqueue(new Callback<ChangePickLocationPostModel>()
call.enqueue(新回调()