网络编程是什么?
网络编程指Android系统使用http协议和服务器交互,以传输或获取相关数据。
WebView
Web控件可以让我们在应用程序中嵌入浏览器,展示网页信息。
修改activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity">
<WebView
android:id="@+id/web_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
声明权限:
<uses-permission android:name="android.permission.INTERNET"/>
修改MainActivity:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webView=findViewById(R.id.web_view);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("https://www.baidu.com");
}
}
HttpURLConnection访问网络
修改activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/send_request"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="send_request" />
<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="wrap_content" />
</ScrollView>
</LinearLayout>
声明权限:
<uses-permission android:name="android.permission.INTERNET"/>
修改MainActivity
- 通过地址创建URL调用openConnection()方法获取HttpURLConnection实例,设置相关参数
- 通过getInputStream()方法获取到返回的数据流并读取解析
- finally调用disconnect()关闭连接
public class MainActivity extends AppCompatActivity {
private TextView responseText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
responseText = findViewById(R.id.response_text);
Button sendRequest = findViewById(R.id.send_request);
sendRequest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendRequestWithHttpURLConnection();
}
});
}
private void sendRequestWithHttpURLConnection() {
new Thread(new Runnable() {
@Override
public void run() {
HttpURLConnection connection = null;
BufferedReader reader = null;
try {
URL url = new URL("https://www.baidu.com");
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(8000);
connection.setReadTimeout(8000);
InputStream inputStream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
showResponse(response.toString());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (connection != null) {
connection.disconnect();
}
}
}
}).start();
}
private void showResponse(final String toString) {
runOnUiThread(new Runnable() {
@Override
public void run() {
responseText.setText(toString);
}
});
}
}
若要向服务器提交数据,则使用POST:
connection.setRequestMethod("POST");
DataOutputStream outputStream=new DataOutputStream(connection.getOutputStream());
outputStream.writeBytes("username=admin&password=123456");
Tips:
- Android不允许在子线程中进行UI操作,需要调用runOnUiThread()方法将线程切换到主线程
- Android28以后不允许明文访问网址,可在Mainfest的application设置android:usesCleartextTraffic=“true” 或设置networkSecurityConfig指定类似如下的配置文件
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="false">
<domain includeSubdomains="true">secure.example.com</domain>
</domain-config>
</network-security-config>
HttpURLConnection最佳用法
一个程序可能在很多地方都需要用到网络,而请求网络的代码是相同的,为避免重复书写代码,我们可以将这部分代码抽出来作为工具类HttpURLConnectionUtil,当需要访问网络时输入地址即可返回数据:
public class HttpURLConnectionUtil {
public static String sendHttpRequest(String address) {
HttpURLConnection connection = null;
try {
URL url = new URL(address);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(8000);
connection.setReadTimeout(8000);
connection.setDoInput(true);
connection.setDoOutput(true);
InputStream inputStream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
return response.toString();
} catch (Exception e) {
e.printStackTrace();
return e.getMessage();
} finally {
if (connection != null) {
connection.disconnect();
}
}
}
}
但存在以下问题:
- 网络属于耗时操作,但sendHttpRequest()方法内并没有开启线程,这样有可能被主线程阻塞
- 但如果在sendHttpRequest()方法内开启线程,可能在服务器还没来得及响应,方法就已经结束了,导致数据无法返回
- 子线程无法通过return返回数据
解决办法是利用回调机制,定义接口:
public interface HttpURLConnectionCallback {
void onFinish(String response);
void onError(Exception e);
}
修改HttpURLConnectionUtil,加入HttpURLConnectionCallback作为参数:
public class HttpURLConnectionUtil {
public static void sendHttpRequest(final String address, final HttpURLConnectionCallback callback) {
new Thread(new Runnable() {
@Override
public void run() {
HttpURLConnection connection = null;
try {
URL url = new URL(address);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(8000);
connection.setReadTimeout(8000);
connection.setDoInput(true);
connection.setDoOutput(true);
InputStream inputStream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
if (callback != null) {
callback.onFinish(response.toString());
}
} catch (Exception e) {
if (callback != null) {
callback.onError(e);
}
} finally {
if (connection != null) {
connection.disconnect();
}
}
}
}).start();
}
}
当使用时,通过onFinish()方法返回数据,onError()方法处理异常:
HttpURLConnectionUtil.sendHttpRequest("https://www.baidu.com", new HttpURLConnectionCallback() {
@Override
public void onFinish(String response) {
}
@Override
public void onError(Exception e) {
}
});
此外,还可以使用开源库OkHttp访问网络.