如何在Android改造上查看响应代码?

时间:2016-10-29 19:01:39

标签: android retrofit

CODE:

Call<ResponseBody> call = postApiService.uploadFile(body, coordinatePoint, textToPost);
call.enqueue(new Callback<ResponseBody>() {
    @Override
    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
        if(response.isSuccessful()){
            Toast.makeText(getContext(), response.code(), Toast.LENGTH_LONG).show();
        }
        else {
            Toast.makeText(getContext(), response.code(), Toast.LENGTH_LONG).show();
        }
    }
    @Override
    public void onFailure(Call<ResponseBody> call, Throwable t) {
        Toast.makeText(getContext(), getString(R.string.check_internet), Toast.LENGTH_LONG).show();
    }

在此代码中,我认为错误在response.code()

因为错误代码是这样的 android.content.res.Resources$NotFoundException: String resource ID #0xc9

Toast.makeText(getContext(), response.code(), Toast.LENGTH_LONG).show();

那么,如何在android retrofit2上获取响应代码?

请你让我知道吗?

2 个答案:

答案 0 :(得分:1)

您收到了aa消息,因为您在*中呼叫yes。这里,第二个参数需要类型为[[ "'ab'" == \'*\' ]] && echo yes # pattern matching, indiv. escaped ' chars. [[ "'ab'" =~ ^\'.*\'$ ]] && echo yes # regex operator =~ 的字符串资源ID,但您要传递响应代码。只需将您的回复代码转换为android.content.res.Resources$NotFoundException: String resource ID #0xc9即可打印出正确的值:

makeText(Context context, int resId, int duration)

答案 1 :(得分:1)

您正在使用的Toast.makeText方法已重载,并使用字符串资源int作为第二个参数。

考虑传递由String.valueOf(response.code())包裹的第二个参数。