詳細參考這個博文:http://www.cnblogs.com/itliucheng/p/5065619.htmlhtml
//關鍵代碼就這幾行
String urlNameString ="http://127.0.0.1:8601/setResult/" + text + "/" + newWordsText;
// //往服務器端寫內容 也就是發起http請求須要帶的參數
// // 根據地址獲取請求
HttpGet request = new HttpGet(urlNameString);//這裏發送get請求
// // 獲取當前客戶端對象
HttpClient httpClient = new DefaultHttpClient();
// // 經過請求對象獲取響應對象
HttpResponse response = httpClient.execute(request);
======================================================================
@RequestMapping("getWeChatUserInfo") @ResponseBody public String getWeChatUserInfo(String token,String openid){ String urlNameString = "https://api.weixin.qq.com/sns/userinfo?access_token=TOKEN&openid=OPENID"; urlNameString=urlNameString.replace("TOKEN", token); urlNameString=urlNameString.replace("OPENID",openid); String result=""; try { // 根據地址獲取請求 HttpGet request = new HttpGet(urlNameString);//這裏發送get請求 // 獲取當前客戶端對象 HttpClient httpClient = new DefaultHttpClient(); // 經過請求對象獲取響應對象 HttpResponse response = httpClient.execute(request); // 判斷網絡鏈接狀態碼是否正常(0--200都數正常) if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { result= EntityUtils.toString(response.getEntity(),"utf-8"); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return result; //....result是用戶信息,站內業務以及具體的json轉換這裏也不寫了... }
//要調用的接口形式 @GetMapping("/setResult/{word}/{classifyName}") public Result<String> setResult(@PathVariable("word") String word, @PathVariable("classifyName") String classifyName) { int i1 = classifyService.selectClassifyCountByName(classifyName);
//接口形式 // @ApiOperation("測試選擇數據並添加分類接口,word須要分類的文本,classifyName添加分類") @PostMapping("/setResult") public Result<String> setResult(@RequestParam String word,@RequestParam String classifyName) { int i1 = classifyService.selectClassifyCountByName(classifyName); if (i1 > 0) { //分類已存在 Classify classify = classifyService.selectClassifyByName(classifyName); KeyWord keyword = new KeyWord(); keyword.setKeywordName(word); keyword.setClassifyId(classify.getId()); classifyService.addKeyWord(keyword);//添加KeyWord數據 modelExercise();//更新本地硬盤的表
//關鍵代碼就這幾行
String urlNameString = "http://127.0.0.1:8601/setResult"; //POST的URL HttpPost httppost = new HttpPost(urlNameString); //創建HttpPost對象 List<NameValuePair> params = new ArrayList<NameValuePair>(); //創建一個NameValuePair數組,用於存儲欲傳送的參數 params.add(new BasicNameValuePair("word", word)); params.add(new BasicNameValuePair("classifyName", classifyName)); //添加參數 httppost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); //設置編碼 HttpResponse response = new DefaultHttpClient().execute(httppost); //發送Post,並返回一個HttpResponse對象 if (response.getStatusLine().getStatusCode() == 200) {//若是狀態碼爲200,就是正常返回 String result = EntityUtils.toString(response.getEntity()); }