使用Retrofit的简单登录表单

Car*_*s J 4 android retrofit2

我开始使用Retrofit,但我坚持这个简单的步骤.我有一个登录表单,我正在尝试使用服务器进行身份验证,但我无法发送请求.

这就是我尝试过的:

我的改造客户:

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);
}
Run Code Online (Sandbox Code Playgroud)

我的登录界面:

public interface Login {

    @POST(LOGIN)
    Call<String> loginWithCredentials(@Body LoginCredentials data);
}
Run Code Online (Sandbox Code Playgroud)

LoginCredentials类:

public class LoginCredentials {

    private String name;
    private String pass;

    public LoginCredentials(String name, String pass) {
        this.name = name;
        this.pass = pass;
    }
}
Run Code Online (Sandbox Code Playgroud)

我称之为的部分:

@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());
        }
    });

}
Run Code Online (Sandbox Code Playgroud)

我一直在收到错误,Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $但我不知道这意味着什么.

Car*_*s J 8

我想到了.这比我想象的容易.您可以Retrofit通过将方法的响应类型设置为,跳过解析ResponseBody.他们只需要阅读响应并使用string()提供的方法.而已!

例:

public interface Login {
    @POST(LOGIN)
    Call<ResponseBody> loginWithCredentials(@Body LoginCredentials data);
}
Run Code Online (Sandbox Code Playgroud)

然后像这样使用:

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
    }
});
Run Code Online (Sandbox Code Playgroud)