Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple login form with Retrofit

I'm starting to work with Retrofit but I'm stuck on this simple step. I have a login form and I'm trying to authenticate with the server but I just can't send the request.

This is what I've tried:

My Retrofit client:

private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder();

private static Retrofit.Builder builder =
        new Retrofit.Builder()
                .baseUrl(API_BASE_URL)
                .addConverterFactory(GsonConverterFactory.create());

public static <S> S createService(Class<S> serviceClass) {
    Retrofit retrofit = builder.client(httpClient.build()).build();
    return retrofit.create(serviceClass);
}

My login interface:

public interface Login {

    @POST(LOGIN)
    Call<String> loginWithCredentials(@Body LoginCredentials data);
}

The LoginCredentials class:

public class LoginCredentials {

    private String name;
    private String pass;

    public LoginCredentials(String name, String pass) {
        this.name = name;
        this.pass = pass;
    }
}

And the part where I call it:

@Override
public void onClick(View v) {

    showProgress(true);

    String username = userField.getText().toString();
    String password = passField.getText().toString();

    ApiController.Login loginClient = ApiController.createService(ApiController.Login.class);
    Call<String> call =loginClient.loginWithCredentials(new LoginCredentials(username, password));
    call.enqueue(new Callback<String>() {
        @Override
        public void onResponse(Call<String> call, Response<String> response) {
            handleResponse(response.body());
        }

        @Override
        public void onFailure(Call<String> call, Throwable t) {
            showProgress(false);
            Log.e(TAG, t.getLocalizedMessage());
        }
    });

}

And I keep getting the error Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $but I have no idea what it means.

like image 313
Carlos J Avatar asked Jan 06 '23 05:01

Carlos J


1 Answers

I figured it out. It was easier than I thought. You can skip the parsing that Retrofit does by setting the response type of the method as ResponseBody. Them you just need to read the response and use the string() method that provides. That's it!

Example:

public interface Login {
    @POST(LOGIN)
    Call<ResponseBody> loginWithCredentials(@Body LoginCredentials data);
}

Then use like this:

String username = userField.getText().toString();
String password = passField.getText().toString();

ApiController.Login loginClient = ApiController.createService(ApiController.Login.class);
Call<ResponseBody> call = loginClient.loginWithCredentials(new LoginCredentials(username, password));
call.enqueue(new Callback<ResponseBody>() {
    @Override
    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
        if (response.isSuccessful()) {
            try {
                // get String from response
                String stringResponse = response.body().string();
                // Do whatever you want with the String
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onFailure(Call<ResponseBody> call, Throwable t) {
        // handle error
    }
});
like image 89
Carlos J Avatar answered Feb 11 '23 17:02

Carlos J