本博文主要是学习如何在手机端使用HTTP协议和服务器端进行网络交互,并对服务器返回的数据进行解析,这也是Android中最常使用到的网络技术。
目录
WebView的用法
有时候我们可能会碰到一些比较特殊的需求,比如说要求在应用程序里展示一些网页。相信每个人都知道,加载和显示网页通常都是浏览器的任务,但是需求里又明确指出,不允许打开系统浏览器,而我们当然也不可能自己去编写一个浏览器出来,这时应该怎么办呢?
借助WebView控件,我们就可以在自己的应用程序里嵌入一个浏览器,从而非常轻松地展示各种各样的网页。下面创建一个WebViewTest项目。修改activity_main.xml中的代码,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<WebView
android:id="@+id/web_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
可以看到,我们在布局文件中使用到了一个新的控件:WebView。这个控件当然也就是用来显示网页的了,这里的写法很简单,给它设置了一个id,并让它充满整个屏幕。
然后修改MainActivity中的代码,如下所示:
package com.example.webviewtest;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//首先使用findViewById() 方法获取到了WebView的实例
WebView webView=(WebView) findViewById(R.id.web_view);
//调用WebView的getSettings() 方法可以去设置一些浏览器的属性
//调用了setJavaScriptEnabled() 方法来让WebView支持JavaScript脚本
webView.getSettings().setJavaScriptEnabled(true);
//调用了WebView的setWebViewClient() 方法,并传入了一个WebViewClient的实例
webView.setWebViewClient(new WebViewClient());
//这代码的作用是:当需要从一个网页跳转到另一个网页时,
// 希望目标网页仍然在当前WebView中显示,而不是打开系统浏览器。
webView.loadUrl("http://www.baidu.com");
//调用WebView的loadUrl() 方法,并将网址传入
}
}
另外还需要注意,由于本程序使用到了网络功能,而访问网络是需要声明权限的,因此还得修改AndroidManifest.xml文件,并加入权限声明,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.webviewtest">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
运行结果如下所示
这是由于Android9.0对未加密的流量不在信任,添加了新的限制。
在Android 的mainfest.xml中的application添加一句配置
android:usesCleartextTraffic="true"
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.webviewtest">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:usesCleartextTraffic="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
就ok了(大多数权限的问题,都是通过AndroidManifest.xml文件解决,所以要注意权限问题的变化)