@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<ResponseBody> call = postRoute.Login(body); // call.enqueue(new Callback<ResponseBody>() { // @Override // public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { // try { // Log.i("test", "onResponse--->" + response.body().string()); // } catch (IOException e) { // e.printStackTrace(); // } // } // // @Override // public void onFailure(Call<ResponseBody> 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<ResponseBody>() { @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<ResponseBody> Login(@Body RequestBody loginReq); @POST("jewel-api/api/security/login") Observable<ResponseBody> 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库在Android中发送JSON格式的POST请求。通过创建Retrofit实例,定义接口,构建RequestBody并调用服务方法,实现了登录功能。在登录失败时,捕获了HttpException以处理错误响应。
3728

被折叠的 条评论
为什么被折叠?



