Http協(xié)議工作原理 :
客戶端向服務器端發(fā)送一條HTTP請求,服務器收到請求之后會返回一些數(shù)據(jù)給客戶端臼婆,然后客戶端再對這些返回的數(shù)據(jù)進行解析和處理
手動發(fā)送Http請求(發(fā)送部分)
1.使用HttpURLConnection
首先要獲取到一個HTTPURLConnection實例缸沃,先new出一個URL對象恰起,
URL url = new URL("www.baidu.com") //傳入目標網(wǎng)站的網(wǎng)絡地址
然后再調(diào)用openConnection()方法,結合起來的寫法是
HttpURLConnection connection? = ? (HttpURLConnection) url.openConnection;
得到HttpURLConnection實例之后和泌,我們可以設置一些關于Http請求的方法村缸,常用的方法選擇有兩個
1.GET? //希望從服務器獲取數(shù)據(jù)
2. POST ? / /希望提交數(shù)據(jù)給服務器
調(diào)用方式為? connection.setRequestMethod("GET")
當然還可以對Http請求做一些自由的定制
例如顯示連接超時和讀取超時的秒數(shù)
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
接收讀取服務器返回的輸入流
調(diào)用getInputStream()方法獲取服務器返回的輸入流
InputStream in = connection.getInputStream();
關閉Http
最后調(diào)用disconnection()方法把Http連接關掉
connection.disconnection;
下面簡單講一個發(fā)送Http請求并接受服務器返回的數(shù)據(jù)小例子
2.使用OkHttp
在使用OkHttp之前,先要在build.gradle添加依賴庫
compile 'com.squareup.okhttp3:okhttp:3.4.1' //后面的3.4.1是okhttp的版本號
接下來來看OkHttp的具體用法
首先創(chuàng)建一個OkHttpClient 實例
OkHttpClient client = new OkHttpClient();
然后創(chuàng)Request對象
Request request = new Request.Builder.url("http://www.baidu.com").build();
之后調(diào)用OkHttpClient的newCall()方法創(chuàng)建一個Call對象武氓,并調(diào)用execute()方法
Response response = client.newCall(request).execute();
Response 的對象 response 就是服務器返回的數(shù)據(jù)
但是我們要得到具體的內(nèi)容梯皿,這樣寫
String responseData = response.body().string();
例子如下
public class MainActivityextends AppCompatActivityimplements View.OnClickListener {
TextViewresponseText;
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? Button sendRequest = (Button) findViewById(R.id.send_request);
? ? ? ? responseText = (TextView) findViewById(R.id.response_text);
? ? ? ? sendRequest.setOnClickListener(this);
? ? }
@Override
? ? public void onClick(View v) {
if (v.getId() == R.id.send_request) {
//在Send Request按鈕的點擊事件中調(diào)用了sendRequestWithHttpURLConnection();方法
? ? ? ? ? ? sendRequestWithOkHttp();
? ? ? ? }
}
private void sendRequestWithOkHttp() {
//開啟線程來發(fā)送網(wǎng)絡請求
? ? ? ? new Thread(new Runnable() {
@Override
? ? ? ? ? ? public void run() {//先開啟一個子線程
? ? ? ? ? ? ? ? try {
//使用OkHttp
? ? ? ? ? ? ? ? ? ? OkHttpClient client =new OkHttpClient(); //創(chuàng)建一個OkHttpClient對象
? ? ? ? ? ? ? ? ? ? Request request =new Request.Builder().url("http://www.baidu.com").build(); //創(chuàng)建一個Request對象并且調(diào)用Builder().url()
? ? ? ? ? ? ? ? ? ? Response response = client.newCall(request).execute();? //使用client對象來調(diào)用newCall()方法獲取服務器返回的數(shù)據(jù)
? ? ? ? ? ? ? ? ? ? String responseData = response.body().string();
? ? ? ? ? ? ? ? ? ? showResponse(responseData);
? ? ? ? ? ? ? ? }catch (Exception e) {
e.printStackTrace();
? ? ? ? ? ? ? ? }
}
}).start();
? ? }
private void showResponse(final String response) {
//將子線程切換為主線程
//android是不允許在子線程進行UI操作的
? ? ? ? runOnUiThread(new Runnable() {
@Override
? ? ? ? ? ? public void run() {
//在這里進行UI操作,將結果顯示在頁面上
? ? ? ? ? ? ? ? responseText.setText(response);
? ? ? ? ? ? }
});
? ? }
}
最后就是請求網(wǎng)絡權限了
<use-permission android:name = "android.permission.INTERNET"/>