我的问题是,我不知道如何开始使用Retrofit 2.0与接收的API – 下面提到…
首先,我需要用户名,密码,fbID(可选),gmailID(可选),twitID(可选),性别,birthDate,位置(不需要 – 如果long和lat有值),经度(可选),纬度(可选),profileImage(可选).
当所有参数都很好 – 接收状态=真.
如果没有 – 接收状态= false,并且所需的参数错误(例如邮件已被占用)
所以我可以收到
status = true
要么
status = false和最多5个参数(用户名,电子邮件,birthDate)的数组.
我试过这个API接口:
public interface AuthRegisterUserApi { @PUT() Call<AuthRegisterUserModel> getStatus( @Field("username") String username,@Field("email") String email,@Field("password") String password,@Field("fbID") String fbID,@Field("gmailID") String gmailID,@Field("twitID") String twitID,@Field("gender") String gender,@Field("birthDate") String birthDate,@Field("location") String location,@Field("longitude") String longitude,@Field("latitude") String latitude,@Field("profileImage") String profileImage ); class Factory { private static AuthRegisterUserApi service; public static AuthRegisterUserApi getInstance() { Retrofit retrofit = new Retrofit.Builder() .baseUrl(ApiConstants.REGISTER_URL) .addConverterFactory(GsonConverterFactory.create()) .build(); service = retrofit.create(AuthRegisterUserApi.class); return service; } } }
with this API models (Pastebin.com)
而这段代码在Activity中:
AuthRegisterUserApi.Factory.getInstance() .getStatus( usernameEditText.getText().toString(),emailEditText.getText().toString(),passwordEditText.getText().toString(),"",(maleRadioButton.isChecked() ? "male" : "female"),mYear + "-" + mMonth+1 + "-" + mDay,(geoLocationToggleButton.isChecked() ? geoLocationEditText.getText().toString() : ""),(!geoLocationToggleButton.isChecked() ? "50" : ""),"") .enqueue(new Callback<AuthRegisterUserModel>() { @Override public void onResponse(Call<AuthRegisterUserModel> call,Response<AuthRegisterUserModel> response) { if(response.isSuccessful()) { if (response.body().isStatus()) { showToast(getApplicationContext(),"Registration ok."); } else { response.body().getInfo().getUsername(); } } } @Override public void onFailure(Call<AuthRegisterUserModel> call,Throwable t) { } });
我有错误:java.lang.IllegalArgumentException:@Field参数只能与表单编码一起使用. (参数#1)用于方法AuthRegisterUserApi.getStatus
我尝试使用Postman注册用户,当我使用选项Body – >的X WWW窗体-urlencoded.
如何创建我的注册查询到这个API?将@Field更改为其他内容?我总是有这个错误…
编辑:需要更改API接口为此:
public interface AuthRegisterUserApi { @FormUrlEncoded @PUT("/api/register") Call<AuthRegisterUserModel> getStatus( @Field("username") String username,@Field("profileImage") String profileImage ); class Factory { private static AuthRegisterUserApi service; public static AuthRegisterUserApi getInstance() { Retrofit retrofit = new Retrofit.Builder() .baseUrl(ApiConstants.BASE_URL) .addConverterFactory(GsonConverterFactory.create()) .build(); service = retrofit.create(AuthRegisterUserApi.class); return service; } } }
BASE_URL = http:// ip_address:8400 …
但是仍然有错误:Response.rawResponse = Response {protocol = http / 1.1,code = 400,message = Bad Request,url = http:// ip_address:8400 / api / register}.使用相同数据的Postman,我收到code = 201 Created.不知道为什么
解决方法
你的请求不是正确的编码,而是邮递员,所以改变:
@FormUrlEncoded @PUT("/api/register") Call<AuthRegisterUserModel> getStatus( @Field("username") String username,@Field("profileImage") String profileImage);
告诉我,如果没关系