我是Adroid开发的新手。我有一个登录表单,我必须发布到服务器。我正在使用Retrofit 2库。我得到响应代码= 400,消息=错误请求。我究竟做错了什么? 这是我的界面:
public interface LoginService {
@POST("login")
Call<Login> createLogin(@Body Login login);
}
我的活动:
Login login = new Login(email, password);
Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl("https://xxxxxx.herokuapp.com/")
.addConverterFactory(GsonConverterFactory.create());
Retrofit retrofit = builder.build();
LoginService loginClient = retrofit.create(LoginService.class);
Call<Login> call = loginClient.createLogin(login);
call.enqueue(new Callback<Login>() {
@Override
public void onResponse(Call<Login> call, Response<Login> response) {
Log.d("response",response.toString());
}
@Override
public void onFailure(Call<Login> call, Throwable t) {
Toast.makeText(LoginActivity.this,":(",Toast.LENGTH_SHORT).show();
}
});
我的登录课程:
public class Login {
private String email;
private String password;
public Login(String email, String password){
this.email = email;
this.password = password;
}
登录时,app必须发送json,如
{"email" : "x@y.z","password" : "abc",}
提前谢谢!