@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 + '\'' + '}'; } }