客戶端與伺服器介面的互動。
阿新 • • 發佈:2019-02-17
DefaultHttpClient httpClient = new DefaultHttpClient();//http://tulogin.erzao.org/tu-login/_____localhost:8080 HttpPost method = new HttpPost("http://localhost:8080/tu-login/tuloginMethod"); JSONObject jsonParam = new JSONObject(); jsonParam.put("phone", phone); jsonParam.put("password", password); jsonParam.put("requrl", requrl); StringEntity entity = new StringEntity(jsonParam.toString(), "utf-8"); entity.setContentEncoding("UTF-8"); entity.setContentType("application/json"); method.setEntity(entity); HttpResponse resGet;
上面的程式碼是設定與伺服器連線的,包括連線伺服器的方法,以及傳輸資料的格式,可編碼方式等。
下面的程式碼是處理伺服器的返回值的。
try { //通過DefaultHttpClient 來獲得返回的引數(值) resGet = httpClient.execute(method); //將返回值設定編碼格式,(避免亂碼) String resData = EntityUtils.toString(resGet.getEntity(),"utf-8"); //通過net.sf.json.JSONObject 來解析字串 JSONObject resJSON = JSONObject.fromObject(resData); //把json中的user物件獲取,並強轉。 Object userjson = resJSON.get("user"); String userString = userjson.toString(); //通過com.google.gson.Gson 來處理json 型別的user物件。 Gson gson = new Gson(); //user就是轉換後的物件。 //在這裡,我對com.google.gson.JsonObject有點疑問,為啥這個不能解析返回的字串呢? //這個類有什麼作用? JsonObject jsonObj = new JsonObject(); jsonObj.getAsJsonObject(resData); if("A00000".equals(resJSON.get("code"))){ User user = gson.fromJson((String) userString, User.class); String token = (String)resJSON.get("token"); securityUtil.addCookieToken(request,response,token,user.getId()); request.getSession().setAttribute("user", user); } if ( !"A00000".equals(resJSON.get("code"))) { result = resJSON.get("data"); }/*else{ result = securityUtil.getReqURL(request,response); }*/ resultCode = resJSON.get("code"); } catch (ClassCastException e) { logger.error(e.getMessage()); resultCode = ResultCode.INTERNAL_ERROR; result = e.getMessage(); }catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
再下面是伺服器處理請求的。
對傳入的JSON進行解析,並對其進行應有的操作。@RequestMapping(value = "/tuloginMethod") public void login(@RequestBody Map<String, Object> map, HttpServletRequest request, HttpServletResponse response) throws UnAuthedException { response.setCharacterEncoding("UTF-8"); String phone = (String) map.get("phone"); String password = Md5Util.md5((String) map.get("password")); ResultCode resultCode = ResultCode.SUCCEED; Object result = null; User user = null; String token = null; try { ParamChecker.notEmpty("phone", phone); ParamChecker.notEmpty("password", password); user = UserService.login(request, response, phone, password); token = (String) request.getAttribute("token"); } catch (UnAuthedException e) { logger.error(e.getMessage()); resultCode = e.getResultCode(); result = e.getMessage(); } catch (Exception e) { logger.error(e.getMessage(), e); resultCode = ResultCode.INTERNAL_ERROR; result = e.getMessage(); } setResponseMethod(response, resultCode, result, user , token); }
並返回值。
下面的程式碼是最後返回的時候。
protected void setResponseMethod(HttpServletResponse resp, ResultCode resultCode,
Object result,User user ,String token , String callback) {
try {
resp.setCharacterEncoding("utf-8");
PrintWriter out = resp.getWriter();
Map<String, Object> ret = new LinkedHashMap<String, Object>();
ret.put("code", resultCode.getCode());
ret.put("data", result);
ret.put("user", user);
ret.put("token", token);
String responseStr = GSON.toJson(ret);
out.println(responseStr);
out.close();
resp.setCharacterEncoding("utf-8");
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}
OK,這樣,客戶端與伺服器進行互動,傳入引數到伺服器中進行處理,並返回值到客戶端進行處理。