0

I am not able to receive token form my REST API . I used Interceptors too but unable to access it. Everytime I test run the app , It just shoes me the toast message inside the onFailure() method inside the enqueue method of Retrofit Can anyone please help me out from this. Any help is appreciated !!! Here's the code.

Retrofit:

public static final String BASE_URL = "http://192.168.0.105:8000/";
public static Retrofit retrofit = null;



public static Retrofit getApiClient(){
    if(retrofit == null){
        //OkHttp Client Instance for using Interceptors
        OkHttpClient.Builder okhttpBuilder = new OkHttpClient.Builder();
        okhttpBuilder.addInterceptor(new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {

                Request request = chain.request();
                Request.Builder newRequest = request.newBuilder().header("Authorization","token");

                return chain.proceed(newRequest.build());
            }
        });


        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .client(okhttpBuilder.build())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    return retrofit;
}

Login Method inside Activtiy:

   private fun login(email:String, password:String) {
    val init_login : Login = Login(email,password)
    val call:Call<ResponseBody> =  apiInterface.login(init_login)
    call.enqueue(object :Callback<ResponseBody>{
        override fun onFailure(call: Call<ResponseBody>?, t: Throwable?) {
            Log.i(LOG_TAG,"Login Failed")
            toast("Login Failed")
        }

        override fun onResponse(call: Call<ResponseBody>?, response: Response<ResponseBody>?) {
            prefConfig.saveToken(response?.body()!!.string())
            Log.i(LOG_TAG, "onResponse -> "+response.body().toString())
            toast(response?.body()!!.string())
        }

    })

Retrofit call:

@POST("/login/")
Call<ResponseBody> login(@Body Login login);

Login Parameter used in Call:

public class Login {

@SerializedName("username")
private String user;

@SerializedName("password")
private String password;

public Login(String user,String password){
    this.user = user;
    this.password = password;
}

}

REST API:

{
"password": [
    "This field is required."
],
"username": [
    "This field is required."
]
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Dilip Krishna
  • 139
  • 3
  • 16

1 Answers1

0

Change this

@POST("/login/")
Call<ResponseBody> login(@Body Login login);

To

@POST("login")
Call<ResponseBody> login(@Body Login login);
prashant17
  • 1,520
  • 3
  • 14
  • 23
  • Not working either!!! The result always comes from onFailure method.Iam anot able to figure out what's wrong? – Dilip Krishna Aug 22 '18 at 07:53
  • Login Failed message in logcat and also toast. That's it! Nothing else. – Dilip Krishna Aug 22 '18 at 07:57
  • add `HttpLoggingInterceptor` to your retrofit for errors check here https://futurestud.io/tutorials/retrofit-2-enable-logging-for-development-builds-only – prashant17 Aug 22 '18 at 07:59
  • Change `Log.i(LOG_TAG,"Login Failed")` to `Log.e(LOG_TAG, "Login Failed", t)`. It will print the stacktrace to logcat and give you/us a better indicator on what's wrong. – Kilian Aug 22 '18 at 08:23
  • D/OkHttp: --> END POST (45-byte body) D/OkHttp: <-- HTTP FAILED: java.net.NoRouteToHostException: No route to host I/MainActivity: Login Failed W/System.err: java.net.NoRouteToHostException: No route to host this is the error showing!!! – Dilip Krishna Aug 22 '18 at 08:26
  • Are you sure `http://192.168.0.105:8000/` is reachable from the device you're using? – Kilian Aug 22 '18 at 08:35
  • I am running development server from my laptop .I got that from using ipconfig command . – Dilip Krishna Aug 22 '18 at 08:48