Pasindu Weerakoon
Pasindu Weerakoon

Reputation: 628

Android Retrofit2 returning not found(404 error)

I'm creating an android application with Retrofit2. It was worked successfully, but today I changed the response object(LoginResponse.java). Now it's returning "not found (404)" error. I think the error will occur from here response.isSuccessful(). I have tried several hours to solve this problem, but couldn't find a way. please help me to solve this. Thanks in advance.

here is my code:

Form backend (Postman response like this)

{
    "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();

                    }
                });

            }
        });

Here is my 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

enter image description here

Upvotes: 3

Views: 3699

Answers (1)

Prashant singh
Prashant singh

Reputation: 113

As you told I am asuming that the if (response.isSuccessful()) condition is not true and the control is going to the else condition in OnResponse method and error you are getting not found (404) is because of response.message() +" "+ response.code() in your else condition of OnResponse method.

Going back to your NetworkInterface.java, just check that the fields you provided below in the dologin methods matches the fields in your backend code sometimes different names creates conflicts. This line may be the culprit of the situation go and varify it with your backend.

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

Upvotes: 2

Related Questions