Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Login via Rest web service from android using retrofit not working

I am making an android app using Retrofit 2. My REST Api are all written in Liferay. Now in Liferay, what I have seen is, to access the web services we need to authenticate first. So i have authenticated like this

http://test:[email protected]:8080/liferay-portlet/api/secure/jsonws/

Liferay has its own user authentication method which we have overridden.I checked the Web service call from Postman its working fine.

URL:http://test:[email protected]:8080/liferay-portlet/api/secure/jsonws/customuserauthentication/authenticate-by-user-name

form-encoded values

companyId:10154
screenName:xyz
password:xyz
active:true

If i put this in the postman, it fetches the json response properly.

Now when i call the same from my android code i get a response "Unauthorized".

My Retrofit service

public interface LoginApi {    
    @FormUrlEncoded
    @POST("/liferay-portlet/api/secure/jsonws/customuserauthentication/authenticate-by-user-name")
    Call<User> login(@Field("companyId")long companyId,@Field("screenName")String screenName,@Field("password")String password,@Field("active")boolean active);
}

My RestApiManager Class(This class is used to call the service interface and create the retrofit builder)

public class RestApiManager {

    private LoginApi loginApi;

    public LoginApi login() {
        if (loginApi==null) {
            GsonBuilder gson=new GsonBuilder();
            gson.registerTypeAdapter(String.class, new StringDeserializer());
            Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("http://test:[email protected]:8080")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
            loginApi=retrofit.create(LoginApi.class);
    }
    return loginApi;
}

A call to the RestApiManager

Call<User> callUser=restApiManager.login().login(loginData.getCompanyId(),loginData.getScreenName(),loginData.getPassword(),loginData.isActive());
callUser.enqueue(new Callback<User>() {
    @Override
    public void onResponse(Response<User> response, Retrofit retrofit) {
        Log.d("Login","Login Response:"+response.body());
    }

    @Override
    public void onFailure(Throwable t) {
        Log.d("Login","Login Response:"+t.getMessage());
    }
});
like image 283
Deb Avatar asked Nov 04 '15 14:11

Deb


People also ask

Is retrofit RESTful?

Retrofit is type-safe REST client for Android and Java which aims to make it easier to consume RESTful web services.

How do you pass body in GET request retrofit on Android?

This means your @GET or @DELETE should not have @Body parameter. You can use query type url or path type url or Query Map to fulfill your need. Else you can use other method annotation.


1 Answers

It looks like perhaps your request should have a JSON body instead of a POST variables? You are calling a JSON webservice and your example parameters look more JSON than POST. If so, then you can encapsulate your parameters in an object --

public class User {
    int companyId;
    String screenName;
    String password;
    boolean active;

    User(int companyId, String screenName, String password, boolean active) {
        this.companyId = companyId;
        this.screenName = screenName;
        this.password = password;
        this.active = active;
}

Your interface would be --

public interface LoginApi {    
    @POST("/liferay-portlet/api/secure/jsonws/customuserauthentication/authenticate-by-user-name")
    Call<User> login(@Body User user);
}

and construct your call as --

User user = new User(loginData.getCompanyId(),loginData.getScreenName(),loginData.getPassword(),loginData.isActive());
Call<User> callUser = restApiManager.login().login(user);
like image 158
iagreen Avatar answered Oct 22 '22 22:10

iagreen