android - 在 android 中使用改造登录
问题描述
我正在尝试通过retrofit
. 我只需要发送手机号码。当我使用postman
身体时,它会得到一个输出。但是当我用 android 调用时,它会得到一个如下所示的错误 json
{
"error": "Validation error",
"error_code": "001",
"Validation_errors": {
"mobile": "<p>The Mobile field is required.</p>"
}
}
HomeActivity.class
ApiInterface apiService =
ApiClient.getClient().create(ApiInterface.class);
Map<String,String> user = new HashMap<>();
user.put("mobile",username.getText().toString().trim());
Call<ResponseBody> mService = apiService.loginwithno(user);
Log.d("TAG", "response: " + mService.toString());
mService.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if (response.isSuccessful()) {
try {
String result = response.body().string();
JSONObject mJsonObject = new JSONObject(result);
Log.d("TAG", "response: " + mJsonObject.toString());
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else {
buttonVisible();
username.setError("Please try again");
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
call.cancel();
buttonVisible();
Snackbar snackbar = Snackbar.make(buttonLogin,
"Please check your internet connection", Snackbar.LENGTH_LONG);
snackbar.show();
}
ApiClient
public class ApiClient {
public static final String BASE_URL = "http://nast.in/driverpool/api/index.php/";
private static Retrofit retrofit = null;
public static Retrofit getClient() {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
return retrofit;
}
}
API接口
public interface ApiInterface {
@POST("account/login?")
Call<ResponseBody> loginwithno(@Body Map<String, String> mobile);
@POST("account/verifyotp")
Call<ResponseBody> verifyotp(@Body HashMap<String, String> mobile);//Param name: mobile, otp
@POST("account/resendotp")
Call<ResponseBody> resentotp(@Body HashMap<String, String> mobile);
}
解决方案
First you don't need '?' in your api and i think you must send json in your @body so create class like this
public class SendLoginData{
@SerializedName("mobile")
public String mobile;
public SendLoginData(String mobile) {
this.mobile = mobile;
}
}
And use it in ApiInterface
@POST("account/login")
Call<ResponseBody> loginwithno(@Body SendLoginData post);
推荐阅读
- reactjs - 隐藏在 Elastic Beanstalk 应用程序中可见的页面源?
- python - Python - 从零开始的 SVM 内核和算法
- python - 由于 python 将主机读取为元组而尝试套接字时键入错误
- php - 每次都需要使用 php artisan serve 吗?
- youtube - 频道的视频列表中缺少视频
- excel - Excel 2013 中的代码“无响应”。转换为公式/新代码
- c - 无法交叉编译 mips
- java - 如何使用 Epson JavaPOS ADK 将位图转换为字节
- javascript - :after 元素上的 Angular MouseEvent 为 event.toElement.clientWidth 提供了错误的值
- javascript - 单选按钮值的返回类型是整数还是字符串