首页 > 解决方案 > 在 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);
}

邮递员截图 在此处输入图像描述

标签: androidjsonpostretrofitpostman

解决方案


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

推荐阅读