HttpClient
測試例子
@SpringBootTest
public class HttpClientTest {/*** 測試通過httpclient發送get方式的請求*/@Testpublic void testGET() throws IOException {//創建httpclient對象CloseableHttpClient httpClient= HttpClients.createDefault();//創建請求對象HttpGet httpGet=new HttpGet("http://localhost:8080/user/shop/status");//發送請求,接受響應結果CloseableHttpResponse response=httpClient.execute(httpGet);//獲取服務端返回的狀態碼int statusCode=response.getStatusLine().getStatusCode();System.out.println("服務端返回的狀態碼為:"+statusCode);HttpEntity entity=response.getEntity();String body= EntityUtils.toString(entity);System.out.println("服務端返回的數據為:"+body);//關閉資源response.close();httpClient.close();}/*** 測試通過httpclient發送post方式的請求*/@Testpublic void testPOST() throws JSONException, IOException {//創建httpclient對象CloseableHttpClient httpClient=HttpClients.createDefault();//創建請求對象HttpPost httpPost = new HttpPost("http://localhost:8080/admin/employee/login");JSONObject jsonObject=new JSONObject();jsonObject.put("username","admin");jsonObject.put("password","123456");StringEntity entity=new StringEntity(jsonObject.toString());//指定請求編碼方式entity.setContentEncoding("utf-8");//數據格式entity.setContentType("application/json");httpPost.setEntity(entity);//發送請求CloseableHttpResponse response=httpClient.execute(httpPost);//解析返回結果int statusCode = response.getStatusLine().getStatusCode();System.out.println("響應碼為:"+statusCode);HttpEntity entity1=response.getEntity();String body = EntityUtils.toString(entity1);System.out.println("響應數據為:"+body);//關閉資源response.close();httpClient.close();}
}
微信小程序開發
直接申請使用測試號,記住這兩項
下載微信開發者工具下載 / 穩定版更新日志
下載后打開微信開發者工具創建小程序
入門案例
實例代碼
index.js
// index.js
Page({data: {msg: "hello world",nickName: '',code:'',result:'',},//獲取微信用戶的頭像和昵稱getUserInfo() {wx.getUserProfile({desc: "獲取用戶信息",success: (res) => {console.log(res.userInfo);//為數據賦值this.setData({nickName: res.userInfo.nickName,url: res.userInfo.avatarUrl,});},});},//微信登錄,獲取微信用戶的授權碼wxLogin(){wx.login({success: (res) => {console.log(res.code)this.setData({code:res.code})},})},//發送請求sendRequest(){wx.request({url: 'http://localhost:8080/user/shop/status',method:'GET',success:(res)=>{console.log(res.data)this.setData({result:res.data})}})}
});
index.wxml
<!-- index.wxml -->
<navigation-bar title="Weixin" back="{{false}}" color="black" background="#FFF"></navigation-bar>
<scroll-view class="scrollarea" scroll-y type="list"><view class="container"><!-- <view>{{msg}}</view> --><view><button bindtap="getUserInfo" type="primary">獲取用戶信息</button>昵稱:{{nickName}}<image style="width:100px;height:100px;" src="{{url}}" /></view><view><button bind:tap="wxLogin" type="warn">微信登錄</button>授權碼:{{code}}</view><view><button bindtap="sendRequest" type="primary">發送請求</button>返回數據:{{result}}</view></view>
</scroll-view>
使用微信開發者工具可能遇到的問題
1、頁面空白不顯示
將微信開發者工具升級到最新版
2、微信開發者工具(微信小程序開發工具)寫代碼的時候沒有組件提示補全也沒有代碼縮進(安裝插件)
1、打開vscode安裝插件
這個要安裝2.2.2版本,2.3版本以上無法使用
2、然后打開微信開發者工具的拓展
點擊
導入已安裝的vscode拓展
wxml格式化功能:F1 或者 CMD + Shift + P 輸入 format wxml 命令 或者右鍵菜單,也可以配置 wxmlConfig.onSaveFormat 開啟保存后自動格式化(每次保存代碼后會自動格式化)
3、微信開發者工具獲取微信用戶昵稱與頭像沒有彈窗
//獲取微信用戶的頭像和昵稱getUserInfo() {wx.getUserProfile({desc: "獲取用戶信息",success: (res) => {console.log(res.userInfo);//為數據賦值this.setData({nickName: res.userInfo.nickName,url: res.userInfo.avatarUrl,});},});
把調試基礎庫改為2.27.0以下的版本?
4、微信開發者工具向后端請求后回復 http://localhost:8080 不在以下 request 合法域名列表中
sendRequest(){wx.request({url: 'http://localhost:8080/user/shop/status',method:'GET',success:(res)=>{console.log(res.data)this.setData({result:res.data})}})}
解決?
再次嘗試,解決