带有 Retrofit 的简单登录表单 [英] Simple login form with Retrofit

查看:66
本文介绍了带有 Retrofit 的简单登录表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用 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.

这是我试过的:

我的改造客户:

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

我的登录界面:

public interface Login {

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

LoginCredentials 类:

The LoginCredentials class:

public class LoginCredentials {

    private String name;
    private String pass;

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

我称之为的部分:

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

}

并且我不断收到错误 Use JsonReader.setLenient(true) to accept the malformed JSON at line 1 column 1 path $但我不知道这是什么意思.

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.

推荐答案

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

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!

示例:

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

然后像这样使用:

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

这篇关于带有 Retrofit 的简单登录表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆