Retrofit 标准版post请求 提交jason字符串

  @OnClick(R.id.click)
    public void onViewClicked() {
        login();
    }

    private void login() {

        // String baseUrl = "http://192.168.8.253:8080/jewel-api";
        //  String apiUrl = "/api/security/login";

        LoginReqest reqest = new LoginReqest();
        reqest.accountType = "EMPLOYEE";
        reqest.businessModuleCode = "HDW";
        reqest.warehouseCode = "001";
        reqest.username = "admin";
        reqest.password = "admin";
        String toUploads = new Gson().toJson(reqest);


        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://192.168.8.253:8080/")
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
        ILoginApi postRoute = retrofit.create(ILoginApi.class);
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), toUploads);
//        Call call = postRoute.Login(body);
//        call.enqueue(new Callback() {
//            @Override
//            public void onResponse(Call call, Response response) {
//                try {
//                    Log.i("test", "onResponse--->" + response.body().string());
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//
//            @Override
//            public void onFailure(Call call, Throwable t) {
//
//                Log.i("test", "onFailure" + t.getMessage());
//
//                if(t instanceof HttpException){
//                    ResponseBody body = ((HttpException) t).response().errorBody();
//                    try {
//                        Log.i("test", "error--->" + body.string());
//                    } catch (IOException IOe) {
//                        IOe.printStackTrace();
//                    }
//                }
//            }
//        });

                 postRoute.Login1(body).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {   //密码设置错误时
                        Log.i("test", "onFailure" + e.getMessage()); //onFailureHTTP 400

                        if(e instanceof HttpException){
                            ResponseBody body = ((HttpException) e).response().errorBody();
                            try {i("test", "error--->" + body.string());   //I/test: error--->{"clientIp":"192.168.8.250","error":null,"errorCode":"BUSINESS_EXCEPTION","httpStatus":400,"message":"密码错误,请重试!"}
                            } catch (IOException IOe) {
                                IOe.printStackTrace();
                            }
                        }
                    }

                    @Override
                    public void onNext(ResponseBody responseBody) {//密码正确时
                        try {
                            Log.i("test", "onResponse--->" + responseBody.string());  //onResponse--->"d1b10dc2-63a1-46c3-b44d-1e9def60b00d"
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }
 
  
 
  
public interface ILoginApi {

    @POST("jewel-api/api/security/login")
    Call Login(@Body RequestBody loginReq);


    @POST("jewel-api/api/security/login")
    Observable Login1(@Body RequestBody loginReq);

}

 
  
 
  
public class LoginReqest {
    public String businessModuleCode;//    模块编码(系统模块,最顶级节点的模块编码)
    public String warehouseCode;// 仓库编码,用于判断当前登陆用户是否拥有仓库权限(非必要,如果不存该参数,则不校验)
    public String accountType;//帐户类型,员工:EMPLOYEE
    public String username;//帐户名称
    public String password;//帐户密码

    @Override
    public String toString() {
        return "LoginReqest{" +
                "businessModuleCode='" + businessModuleCode + '\'' +
                ", warehouseCode='" + warehouseCode + '\'' +
                ", accountType='" + accountType + '\'' +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

 
 

你可能感兴趣的:(Retrofit 标准版post请求 提交jason字符串)