我正在使用Retrofit2创建一个android应用程序。它工作成功,但是今天我更改了响应对象(LoginResponse.java)。现在,它返回“ 未找到(404)”错误。我认为该错误将从此处 response.isSuccessful()发生。我已经尝试了几个小时来解决此问题,但找不到方法。请帮助我解决这个问题。预先感谢。
这是我的代码:
表单后端(像这样的邮递员响应)
{
"data": [
{
"token": "sample"
}
],
"msg": "Authentication successfull.",
"status": "success"
}
NetworkClient.java
public static Retrofit getRetrofit() {
if (retrofit == null) {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
OkHttpClient okHttpClient = builder.build();
retrofit = new Retrofit.Builder()
.baseUrl("http://api.acloud.net/")
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(okHttpClient)
.build();
}
return retrofit;
}
LoginResponse.java
@SerializedName("data")
@Expose
private List<TockenResponse> data = null;
@SerializedName("msg")
@Expose
private String msg;
@SerializedName("status")
@Expose
private String status;
public List<TockenResponse> getData() {
return data;
}
public void setData(List<TockenResponse> data) {
this.data = data;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
NetworkInterface.java
@FormUrlEncoded
@POST("auth/login")
Call<LoginResponse> doLogin(@Field("username") String username, @Field("password") String password);
Login.java
private void doLogin(String username, String password) {
NetworkInterface service = NetworkClient.getRetrofit().create(NetworkInterface.class);
Call<LoginResponse> call = service.doLogin(username, password);
call.enqueue(new Callback<LoginResponse>() {
@Override
public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
if (response.isSuccessful()) {
progressDoalog.dismiss();
LoginResponse loginResponse = response.body();
if(loginResponse.getStatus().equals("success")) {
// dataManager.setTocken(response.body().getToken());
Log.d("token2345", "qwertgf");
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
finish();
}else{
Toast.makeText(getApplicationContext(), "Username or Password Incorrect !", Toast.LENGTH_LONG).show();
}
}else {
Toast.makeText(getApplicationContext(), response.message() +" "+ response.code(), Toast.LENGTH_LONG).show();
}
}
@Override
public void onFailure(Call<LoginResponse> call, Throwable t) {
Toast.makeText(getApplicationContext(), "Something went wrong...!", Toast.LENGTH_LONG).show();
}
});
}
});
这是我的Logcat
11-02 12:16:04.531 11501-11501/com.application.mobile.application D/PhoneWindow: *FMB* isFloatingMenuEnabled mFloatingMenuBtn : null
*FMB* isFloatingMenuEnabled return false
11-02 12:16:04.536 11501-12274/com.application.mobile.application D/OkHttp: --> POST http://a.acloud.net/auth/login
Content-Type: application/x-www-form-urlencoded
11-02 12:16:04.541 11501-12274/com.application.mobile.application D/OkHttp: Content-Length: 38
username=gihand&password=your_password
--> END POST (38-byte body)
11-02 12:16:04.576 11501-11582/com.application.mobile.application D/mali_winsys: new_window_surface returns 0x3000, [597x270]-format:1
11-02 12:16:06.081 11501-12274/com.application.mobile.application D/OkHttp: <-- 404 Not Found http://a.acloud.net/auth/login (1537ms)
Date: Fri, 02 Nov 2018 06:46:06 GMT
Server: Apache/2.4.18 (Ubuntu)
0: Content-Type : Application/json
Cache-Control: no-cache, private
Content-Length: 411
Content-Type: application/json
X-Cache: MISS from squid_Wifi
X-Cache-Lookup: MISS from squid_Wifi:3128
Via: 1.1 squid_Wifi (squid/3.5.27)
Connection: keep-alive
{"data":[{"token":"ssdsdsdsdsdsdsdsdfdcsdfdd"}],"msg":"Authentication successfull.","status":"success"}
<-- END HTTP (411-byte body)
11-02 12:16:06.136 11501-11501/com.application.mobile.application D/ViewRootImpl: Buffer Count from app info with ::-1 && -1 for :: com.application.mobile.application from View :: -1 DBQ Enabled ::false false
11-02 12:16:06.176 11501-11582/com.application.mobile.application D/mali_winsys: new_window_surface returns 0x3000, [222x78]-format:1
11-02 12:21:10.401 11501-11501/com.application.mobile.application V/ActivityThread: updateVisibility : ActivityRecord{1b786a90 token=android.os.BinderProxy@29c3be6c {com.application.mobile.application/com.application.mobile.application.ui.login.LoginActivity}} show : true
答案 0 :(得分:1)
正如您所告诉我的那样,假设if (response.isSuccessful())
条件不成立,并且控件正在 OnResponse 方法中转到else条件,并且错误提示您未找到( 404)是因为在您的 OnResponse 方法的else条件下,response.message() +" "+ response.code()
。
回到您的 NetworkInterface.java ,只需检查您在dologin方法中在下面提供的字段是否与您的后端代码中的字段匹配,有时使用不同的名称会产生冲突。这行可能是导致这种情况的罪魁祸首,请与您的后端进行确认。
Call<LoginResponse> doLogin(@Field("username") String username, @Field("password") String password);