首页 > 解决方案 > Android Retrofit2 返回未找到(404 错误)

问题描述

我正在使用 Retrofit2 创建一个 android 应用程序。它工作成功,但今天我更改了响应对象(LoginResponse.java)。现在它返回“未找到(404) ”错误。我认为错误将从这里发生response.isSuccessful()。我已经尝试了几个小时来解决这个问题,但找不到方法。请帮我解决这个问题。提前致谢。

这是我的代码:

表单后端(像这样的邮递员响应)

{
    "data": [
        {
            "token": "sample"
        }
    ],
    "msg": "Authentication successfull.",
    "status": "success"
}

网络客户端.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;
    }

登录响应.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;
    }

网络接口.java

@FormUrlEncoded
    @POST("auth/login")
    Call<LoginResponse> doLogin(@Field("username") String username, @Field("password") String password);

登录.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

在此处输入图像描述

标签: androidjsonretrofit2

解决方案


正如您所说,我假设if (response.isSuccessful())条件不正确,并且控件将转到OnResponse方法中的 else 条件,而您找不到的错误(404)是因为OnResponse方法response.message() +" "+ response.code()的 else 条件。

回到您的NetworkInterface.java,只需检查您在 dologin 方法中提供的字段是否与后端代码中的字段匹配,有时不同的名称会产生冲突。这条线可能是这种情况的罪魁祸首,请用您的后端进行验证。

 Call<LoginResponse> doLogin(@Field("username") String username, @Field("password") String password);

推荐阅读