对于Android开发来说,我们只需要了解一些就足够了。它的工作原理很简单,就是客户端向服务器发送一条HTTP请求,服务器收到请求之后会返回一些数据给客户端,然后客户端再对这些数据进行解析和处理就可以了。
Android上发送HTTP请求一般有两种方式:HttpURLConnection 和 HttpClient。不过由于HttpClient存在API数量过多、扩展困难等缺点,Android团队越来越不建议我们使用这种方式,终于在Android6.0系统中,HttpClient的功能被完全移除了,标志着此功能被正式弃用。
HttpURLConnection的用法:
(如果没了解过的可以先看一下我的另外一篇关于HTTP协议的笔记:Http协议-笔记)
首先要获取到HttpURLConnection 的实例,一般只需要new出一个URL对象,并传入目标的网络地址,然后调用一下openConnection()方法即可,如下所示:
URL url = new URL("http://baidu.com");
HttpURLConnection connection =(HttpURLConnection)url.openConnection();
在得到了HttpURLConnection的实例后,我们可以设置一下HTTP请求所使用的方法。常用的方法主要有两个:GET和POST。GET表示希望从服务器那里获取数据,而POST则表示希望提交数据给服务器。写法如下:
connection.setRequestMethod("GET");
接下来就可以进行一些自由的定制了,比如设置连接超时、读取超时的毫秒数,以及服务器希望得到的一些消息头等。示例写法如下:
connection.setConnectTimeout(10000);
connection.setReadTimeout(10000);
之后再调用getInputStream()方法就可以获取到服务器返回的输入流了,剩下的任务就是对输入流进行读取了,如下所示:
InputStream in = connection.getInputStream();
最后可以调用disconnect()方法将这个HTTP连接关掉,如下所示:
connection.disconnect();
具体的例子代码如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.study.xda.networktest.MainActivity"> <Button android:id="@+id/send_request" android:text="Send Request" android:layout_width="match_parent" android:layout_height="wrap_content" /> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/response_text" android:layout_width="match_parent" android:layout_height="match_parent" /> </ScrollView> </LinearLayout>接着修改MainActivity中的代码:
package com.study.xda.networktest; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private Button sendRequest; TextView responseText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); responseText = findViewById(R.id.response_text); sendRequest = findViewById(R.id.send_request); sendRequest.setOnClickListener(this); } @Override public void onClick(View view) { int id = view.getId(); switch (id){ case R.id.send_request: sendRequestWithHttpURLConnection(); } } private void sendRequestWithHttpURLConnection() { //开启线程来发起网络请求 new Thread(new Runnable() { @Override public void run() { HttpURLConnection connection = null; BufferedReader reader = null; try { URL url = new URL("http://www.baidu.com"); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(5000); //获取输入流 InputStream in = connection.getInputStream(); //下面对获取到的输入流进行读取 reader = new BufferedReader(new InputStreamReader(in)); StringBuilder sb = new StringBuilder(); String line; while ((line = reader.readLine())!=null){ sb.append(line); } showResponse(sb.toString()); } catch (Exception e) { e.printStackTrace(); } finally { if (reader!= null ){ try { //关闭bufferreader reader.close(); } catch (IOException e) { e.printStackTrace(); } } if (connection != null){ //关闭这个HTTP连接 connection.disconnect(); } } } }).start(); } private void showResponse(final String response) { runOnUiThread(new Runnable() { @Override public void run() { //在这里做UI操作,将结果显示到界面上 responseText.setText(response); } }); } }可以看到,在sendrequest按钮的点击事件中,调用了sendRequestWithHttpURLConnection()方法,在这个方法中先是开启了一个子线程,然后在子线程中使用HttpURLConnection发出一条HTTP请求,请求的目标地址就是百度的首页。接着利用BuffererReader对服务器返回的流进行读取,并将结果传入到showResponse()方法中。而在showResponse()方法里则是调用了一个runOnUiThread()方法,然后在这个方法的匿名类参数中进行操作,将返回的数据显示在界面上。在这里用这个runOnUiThread()方法的原因是Android是不允许子线程中进行UI操作的,我们需要通过这个方法将现场切换到主线程,然后再更新UI元素。
<uses-permission android:name="android.permission.INTERNET"/>
添加这行代码。
允许代码,点击SEND REQUEST按钮,运行效果如下:
这个是我在虚拟机上运行的效果,有点小丑,不过不要介意,我们程序员只负责功能实现,好看是UI妹子的事。
除了HttpURLConnection,我们还可以选择别的网络通信库代替原生的HttpURLConnection,其中OkHttp是做得最出色的一个。OkHttp不仅在接口封装上面做得简单易用,就连底层实现上野是自成一派。
在使用OkHttp之前,需要先在项目中添加OkHttp库的依赖。编辑app/build.gradle文件,在dependencies闭包中添加如下内容:
dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:26.1.0' implementation 'com.android.support.constraint:constraint-layout:1.1.0' implementation 'com.squareup.okhttp3:okhttp:3.4.1' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' }
OkHttp的具体用法,首先要创建一个OkHttpClient的实例:
OkHttpClient client = new OkHttpClient();
接下来如果想要发起一条HTTP请求,就需要创建一个Request对象:
Request request = new Request.Builder().build();
上述代码只是创建了一个空的Request对象,并没有什么实际作用,我们可以在最终的build()方法之前连缀很多其他方法来丰富这个Request对象,比如可以用过url()方法来设置目标的网络地址,如下所示:
Request request = new Request.Builder()
.url("http://www.baidu.com")
.build();
之后调用OkHttpClient的newCall()方法来创建一个Call对象,并调用它的execute()方法来发送请求并获取服务器返回的数据,如下所示:
Response response =client.newCall(request).execute();
其中Response对象就是服务器返回的数据了,我们可以使用如下写法来得到返回的具体内容:
String responseData = response.body().string();
如果是发起一条POST请求会比GET请求稍微复杂一点,我们需要先构建出一个RequestBody对象来存放待提交的参数,如下所示:
RequestBody requestBody =new FromBody.Builder()
.add("username","admin")
.add("password","123456")
.build();
然后在Request.Builder中调用一下post()方法,并RequestBody对象传入:
Request request =new Request.Builder()
.url("http://www.baidu.com")
.post(requestBody)
.build();
接下来的操作就和GET请求一样了,调用execute()方法来发送请求并获取服务器返回的数据即可。
现在讲刚才的NetworkTest项目改用OkHttp的方式再实现一遍:
布局文件什么的都不用改,直接改MainActivity中的代码:
@Override public void onClick(View view) { int id = view.getId(); switch (id){ case R.id.send_request: // sendRequestWithHttpURLConnection(); sendRequestWithOkHttp(); } } private void sendRequestWithOkHttp() { new Thread(new Runnable() { @Override public void run() { try { OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder().url("http://www.baidu.com").build(); Response response = client.newCall(request).execute(); String responseData = response.body().string(); showResponse(responseData); } catch (IOException e) { e.printStackTrace(); } } }).start(); }上面的sendRequestWithOkHttp()方法同样是先开启了一个子线程,然后在子线程里使用OkHttp发出一条HTTP请求,运行程序,点击SEND REQUEST按钮后,手机上的显示效果应该是跟HttpURLConnection的运行效果是一样的,这样就表示使用OkHttp来发送HTTP请求的功能以及成功实现了。