在Android中进行改造登录

时间:2018-12-15 13:37:11

标签: android json post retrofit postman

我正在尝试通过retrofit使用登录api。我只需要发送手机号码。当我使用postman主体时,它正在输出。但是当iam用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;
}
}

ApiInterface

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

邮递员屏幕截图 enter image description here

2 个答案:

答案 0 :(得分:0)

首先,您不需要'?'在您的api中,我认为您必须在@body中发送json,因此创建此类

npm WARN tar invalid entry

并在ApiInterface中使用它

public class SendLoginData{
    @SerializedName("mobile")
    public String mobile;

    public SendLoginData(String mobile) {
        this.mobile = mobile;
    }
}

答案 1 :(得分:0)

您需要对代码进行少量更改。

  • 更改登录api以接收此类json,如果尚未在项目中添加,请包含gson库。

    
    @POST("account/login?")
    Call loginwithno(@Body Map mobile);
    
  • 创建一个ApiErrorResponse对象来处理您的api错误。根据需要添加getter,setter和@SerializedName。

    
     class ApiErrorResponse{
        String error;
        String error_code;
        ValidationErrors Validation_errors; 
     } 
     class ValidationErrors{
        String mobile;
     }
     
  • 关于API错误句柄

    
       if(!response.isSuccessful()){
            Converter converter =
                    ApiClient.getClient().responseBodyConverter(ApiErrorResponse.class, new Annotation[0]);
            ApiErrorResponse errors = null;
            try {
                errors = converter.convert(response.errorBody());
            } catch (Exception e) {

        }
        if(errors!=null){
         //Handle your API Error logic here
        }
    

    } if(errors!=null){ //Handle your API Error logic here }