<-- 401 Unauthorized http://172.20.20.128/api/v1/login (126ms)
D/OkHttp: Date: Tue, 19 Sep 2017 12:55:29 GMT
D/OkHttp: Server: Apache/2.4.12 (Unix) OpenSSL/1.0.1m PHP/5.5.24 mod_perl/2.0.8-dev Perl/v5.16.3
D/OkHttp: X-Powered-By: PHP/5.5.24
D/OkHttp: Set-Cookie: PHPSESSID=bl0brh2n6pr1sbp76kf95q8um1; path=/; HttpOnly
D/OkHttp: Expires: Thu, 19 Nov 1981 08:52:00 GMT
D/OkHttp: Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
D/OkHttp: Pragma: no-cache
D/OkHttp: Set-Cookie: language=26fb26f386223e2e177900bfa422048f6e2e5367s%3A5%3A%22en_us%22%3B; expires=Thu, 28-Dec-2017 12:55:29 GMT; Max-Age=8640000; path=/
D/OkHttp: Content-Length: 68
D/OkHttp: Keep-Alive: timeout=5, max=100
D/OkHttp: Connection: Keep-Alive
D/OkHttp: Content-Type: application/json
D/OkHttp: {"error":true,"code":"401","message":"Invalid username or password"}
D/OkHttp:
<-- END HTTP (68-byte body)
以上是在android监视器中显示的日志。我想获得上面的日志详细信息中显示的以下消息。
{"error":true,"code":"401","message":"Invalid username or password"}
这是我的请求
call.validateUser(request)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<Response<LoginResponse>>() {
@Override
public void onCompleted() {
AppLog.info("onCompleted", "onCompleted");
}
@Override
public void onError(Throwable e) {
AppLog.info("onError", new Gson().toJson(e));// not coming to onError.
}
@Override
public void onNext(Response<LoginResponse> loginResponse) {
AppLog.info("code", "" + loginResponse.code()); // returns 401
AppLog.info("message", loginResponse.message()); // returns Unauthorized
LoginResponse response = loginResponse.body(); // returns null
AppLog.info("errorBody", new Gson().toJson(loginResponse.errorBody()));// returns null
AppLog.info("headers", new Gson().toJson(loginResponse.headers()));// returns header detail
AppLog.info("body", new Gson().toJson(loginResponse.body()));// returns null
}
});
我检查了onError回调。它还显示401 Unauthorized消息而不是原始错误消息。
答案 0 :(得分:1)
为什么不能尝试onSucess(); onRespose(); onFailure处();所以coud会给出无效的用户名或密码错误
{{1}}
答案 1 :(得分:0)
转到LoginResponse.java页面并编写我在此处写的代码
if
编写完此代码后,在api响应中编写以下代码
@SerializedName("message")
public String message;
public String getmessage(){return message;}
@SerializedName("code")
public String code;
public String getcode(){return code;}
@SerializedName("error")
public String error;
public String geterror(){return error;}
答案 2 :(得分:0)
尝试Interceptor
,如图所示
compile 'com.squareup.okhttp3:logging-interceptor:x.x.x'
在RestClient类中添加以下内容
private APIInterface service;
public RestClient() {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(logging);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(AppConfiguration.BASEURL)
.addConverterFactory(GsonConverterFactory.create())
.client(httpClient.build())
.build();
service = retrofit.create(APIInterface.class);
}
查看this 了解详情
答案 3 :(得分:0)
你有Observable<Response<LoginResponse>>
。
从https://github.com/square/retrofit/issues/1218
引用jake构建您的observable有三种方法:
1. Observable<BodyType>
2. Observable<Response<BodyType>>
3. Observable<Result<BodyType>>
对于第一个版本,无法挂起非200响应 信息,因此它包含在传递给onError的异常中。对于 后两者,数据被封装在Response对象中 可以通过调用errorBody()来访问。
您可以使用https://square.github.io/retrofit/2.x/retrofit/retrofit2/Response.html#errorBody--
中的loginResponse.errorBody()
您可以登录onNext
if(loginResponse.isSuccessfull() {
// do something
} else {
AppLog.info("errorbody",listResponse.errorBody().string());
}
注意:在回答之前,我尝试了一个返回404状态代码的示例api。 new Gson().toJson(loginResponse.body())
确实返回null。我改为loginResponse.errorBody().string()
,我能够记录错误正文