Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 当改装请求失败时,如何获取改装请求的正文?_Android_Retrofit2_Rx Android_Rx Java2 - Fatal编程技术网

Android 当改装请求失败时,如何获取改装请求的正文?

Android 当改装请求失败时,如何获取改装请求的正文?,android,retrofit2,rx-android,rx-java2,Android,Retrofit2,Rx Android,Rx Java2,我正在使用RxJava进行改装。我想知道当请求失败或成功时,如何检索请求发送的BODY/RAW 这是我调用API的控制器: ChatMessage body = new ChatMessage(); ... //configuration of body variable is omitted. ... chatController.sendMessage(body).subscribe(this::onSendMessageSuccess, this::onSendM

我正在使用RxJava进行改装。我想知道当请求失败或成功时,如何检索请求发送的BODY/RAW

这是我调用API的控制器:

ChatMessage body = new ChatMessage();
    ...
    //configuration of body variable is omitted.
    ...

chatController.sendMessage(body).subscribe(this::onSendMessageSuccess, this::onSendMessageError);
以下是获得答案的方法:

private void onSendMessageSuccess(ChatRestResponse response) {
   // How can I get the "ChatMessage body" sent in at first by the re
}

private void onSendMessageError(Throwable throwable) {
     // How can I get the "ChatMessage body" sent in at first by the request       
}
我想知道如何获取用于发出请求的ChatMessage类

仅供参考

这是我的界面:

@Headers({
     "Accept: application/json",
     "Content-Type: application/json"
})
@POST(URL)
Observable<ChatRestResponse> sendMessage(
     @Header("Authorization") String token,
     @Body ChatMessage body);
@标题({
“接受:应用程序/json”,
“内容类型:应用程序/json”
})
@帖子(URL)
可观察发送消息(
@标题(“授权”)字符串标记,
@正文(信息正文);
这是我调用api的控制器:

public Observable<ChatRestResponse> sendMessage(ChatMessage body) {
     String accessToken = mPref.getAccesToken();

     return mChatApi.sendMessage(accessToken , body)
         .subscribeOn(Schedulers.newThread())
         .observeOn(AndroidSchedulers.mainThread());
}
公共可观察发送消息(ChatMessage正文){
字符串accessToken=mPref.getAccesToken();
返回mChatApi.sendMessage(accessToken,正文)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread());
}

故障时,响应中不存在主体。只有一些代码与它所面临的错误相关联。比如404等等。 但是,如果您谈论的是您自己的服务器响应,那么它将失败。例如,如果您通过改装从应用程序登录到您的帐户。如果您输入了错误的凭据,internet和与服务器的连接都正常,即使如此,响应也将运行函数
onresponse()
。但是在这里,您可以检查服务器将发送的与类型或错误相关联的服务器内部错误

call.enqueue(new Callback<LoginResponse>() {
                    @Override
                    public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
                        if (response.code == HttpURLConnection.HTTP_OK) {
                            //All is well
                            // you can get body here as 
                            String token = responce.body().getToken();
                            //etc
                        } else {
                           //Something went wrong like password etc
                           //You can check the body here also in case of failure
                           //Which is due to some internal server error
                           //because of wrong credentials
                           //But this response is in failure and also have a body 
                           //I guess this is what u want
                           String failuerToken = responce.getBody().getAccessToken();
                            new AlertDialog.Builder(LoginActivity.this)
                                    .setMessage("Invalid Credentials")
                                    .setCancelable(false)
                                    .setPositiveButton("Try Again", null)
                                    .show();
                        }
                    }

                    @Override
                    public void onFailure(Call call, Throwable t) {
                        showProgress(false);

                        new AlertDialog.Builder(LoginActivity.this)
                                .setMessage("Unable to reach server")
                                .setCancelable(false)
                                .setPositiveButton("Try Again", null)
                                .show();
                        call.cancel();
                    }
                });
这是我自己的班级

public class LoginResponse {
    @SerializedName("access_token")
    @Expose
    public String accessToken;
    @SerializedName("token_type")
    @Expose
    public String tokenType;
    @SerializedName("expires_in")
    @Expose
    public Integer expiresIn;
}
更新

Call<LoginResponse> call = apiInterface.Login(requestData);
像这样设置变量值

RequestData.username = "abcd";
RequestData.password = "abcd";
RequestData.email = "abcd@email.com";
RequestData.age = 20";
然后通过
api调用
发送数据


失败后,使用此类从中访问以前的数据,并将其分别显示在UI上

@Deneb Chorny这是完美的答案。。失败时,响应中没有主体,因此您必须使用callback的onFailure方法。答案很好,我已经知道了,谢谢您的回答。但我想知道有没有办法制造拦截器?我是说。如果出现错误或成功,我希望能够获取我作为raw/body发送的对象。为什么?因为我想在创建请求的确切对象中更新UI。我希望有solution@DenebChorny请参见上面答案中的更新。如果我使用静态变量,请想象发出多个请求,这不是rxjavai。出于这个原因,我提出了这个问题,否则就没有必要使用静态变量。
public class RequestData {
    public static String username;
    public static String password;
    public static String email;
    public static Int age;
    //etc
}
RequestData.username = "abcd";
RequestData.password = "abcd";
RequestData.email = "abcd@email.com";
RequestData.age = 20";