目录
一、WebView的用法
如何在应用程序中展示一些网页:
1.修改activity_main.xml中的代码
<WebView
android:id="@+id/web_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
2.修改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支持JavaScript脚本
webView.getSettings().setJavaScriptEnabled(true);
//当需要从一个网页跳转到另一个网页时,目标网页仍然在当前WebView中展示,而不是打开系统浏览器
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("https://www.baidu.com");
}
}
3.修改AndroidManifest.xml文件
<uses-permission android:name="android.permission.INTERNET"/>
二、使用HTTP协议访问网络
1.使用HttpURLConnection
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<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>
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TextView responseText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button sendRequest = findViewById(R.id.send_request);
responseText = findViewById(R.id.response_text);
sendRequest.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (view.getId() == R.id.send_request) {
sendRequestWithHttpUrlConnection();
}
}
private void sendRequestWithHttpUrlConnection() {
new Thread(new Runnable() {
@Override
public void run() {
HttpURLConnection connection = null;
BufferedReader reader = null;
URL url = null;
try {
url = new URL("https://www.baidu.com");
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
//设置连接超时毫秒数
connection.setConnectTimeout(8000);
//设置读取超时毫秒数
connection.setReadTimeout(8000);
//获取服务器返回的输入流
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuilder builder = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
showResponse(builder.toString());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (connection != null) {
//将HTTP连接关掉
connection.disconnect();
}
}
}
}).start();
}
private void showResponse(String response) {
runOnUiThread(new Runnable() {
@Override
public void run() {
responseText.setText(response);
}
});
}
}
2.使用OkHttp
首先在项目中添加OkHttp库的依赖:
implementation("com.squareup.okhttp3:okhttp:4.9.1")
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
...
@Override
public void onClick(View view) {
if (view.getId() == R.id.send_request) {
sendRequestWithOkHttp();
}
}
private void sendRequestWithOkHttp() {
new Thread(new Runnable() {
@Override
public void run() {
try {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("https://www.baidu.com")
.build();
Response response = client.newCall(request).execute();
String s = response.body().string();
showResponse(s);
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
...
}
三、解析XML格式数据
1.Pull解析方式
需要使用模拟器测试
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
...
private void sendRequestWithOkHttp() {
new Thread(new Runnable() {
@Override
public void run() {
try {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
//指定访问的服务器地址是计算机本机,10.0.2.2对于模拟器来说就是计算机本机的IP地址
.url("http://10.0.2.2/get_data.xml")
.build();
Response response = client.newCall(request).execute();
String s = response.body().string();
parseXmlWithPull(s);
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
private void parseXmlWithPull(String xmlData) {
XmlPullParserFactory factory = null;
try {
factory = XmlPullParserFactory.newInstance();
XmlPullParser parser = factory.newPullParser();
parser.setInput(new StringReader(xmlData));
int type = parser.getEventType();
String id = null;
String name = null;
String version = null;
while (type != XmlPullParser.END_DOCUMENT) {
String nodeName = parser.getName();
switch (type) {
case XmlPullParser.START_TAG: {
if ("id".equals(nodeName)) {
id = parser.nextText();
} else if ("name".equals(nodeName)) {
name = parser.nextText();
} else if ("version".equals(nodeName)) {
version = parser.nextText();
}
break;
}
case XmlPullParser.END_TAG: {
if ("app".equals(nodeName)) {
Log.d("TAG", "id is " + id);
Log.d("TAG", "name is " + name);
Log.d("TAG", "version is " + version);
}
break;
}
default:
break;
}
type = parser.next();
}
} catch (Exception e) {
e.printStackTrace();
}
}
...
}
从Android 9.0系统开始,应用程序默认只允许使用HTTPS类型的网络请求,HTTP类型的网络请求因为有安全隐患默认不再被支持,而我们搭建的Apache服务器现在使用的就是HTTP。
那么为了能让程序使用HTTP,我们还要进行如下配置才可以。右击res目录→New→Directory,创建一个xml目录,接着右击xml目录→New→File,创建一个network_config.xml文件。然后修改network_config.xml文件中的内容:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true">
<trust-anchors>
<certificates src="system" />
</trust-anchors>
</base-config>
</network-security-config>
这段配置文件的意思就是允许我们以明文的方式在网络上传输数据,而HTTP使用的就是明文传输方式。
接下来修改AndroidManifest.xml中的代码来启用我们刚才创建的配置文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jack.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/Theme.WebViewTest"
android:networkSecurityConfig="@xml/network_config"
>
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
2.SAX解析方式
public class ContentHandler extends DefaultHandler {
private String nodeName;
private StringBuilder id;
private StringBuilder name;
private StringBuilder version;
//开始XML解析的时候调用
@Override
public void startDocument() throws SAXException {
id = new StringBuilder();
name = new StringBuilder();
version = new StringBuilder();
}
//开始解析某个节点的时候调用
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
nodeName = localName;
// Log.d("TAG", "uri is "+uri);
// Log.d("TAG", "localName is "+localName);
// Log.d("TAG", "qName is "+qName);
// Log.d("TAG", "attributes is "+attributes);
}
//获取节点内容的时候调用
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
if ("id".equals(nodeName)) {
id.append(ch, start, length);
} else if ("name".equals(nodeName)) {
name.append(ch, start, length);
} else if ("version".equals(nodeName)) {
version.append(ch, start, length);
}
}
//完成解析某个节点的时候调用
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if ("app".equals(localName)) {
//trim()不仅可以去掉空格,还能去掉其他一些多余的符号https://zhidao.baidu.com/question/9668427.html
Log.d("TAG", "id is " + id.toString().trim());
Log.d("TAG", "name is " + name.toString().trim());
Log.d("TAG", "version is " + version.toString().trim());
id.setLength(0);
name.setLength(0);
version.setLength(0);
}
}
//完成整个XML解析的时候调用
@Override
public void endDocument() throws SAXException {
super.endDocument();
}
}
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
...
private void sendRequestWithOkHttp() {
new Thread(new Runnable() {
@Override
public void run() {
try {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("http://10.0.2.2/get_data.xml")
.build();
Response response = client.newCall(request).execute();
String s = response.body().string();
parseXmlWithSax(s);
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
private void parseXmlWithSax(String xmlData) {
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
XMLReader reader = factory.newSAXParser().getXMLReader();
ContentHandler handler = new ContentHandler();
reader.setContentHandler(handler);
reader.parse(new InputSource(new StringReader(xmlData)));
} catch (Exception e) {
e.printStackTrace();
}
}
...
}
四、解析JSON格式数据
1.使用JSONObject
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
...
private void sendRequestWithOkHttp() {
new Thread(new Runnable() {
@Override
public void run() {
try {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("http://10.0.2.2/get_data.json")
.build();
Response response = client.newCall(request).execute();
String s = response.body().string();
parseJSONWithJSONObject(s);
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
private void parseJSONWithJSONObject(String dataJSON) {
try {
JSONArray array = new JSONArray(dataJSON);
for (int i = 0; i < array.length(); i++) {
JSONObject object = array.getJSONObject(i);
String id = object.getString("id");
String name = object.getString("name");
String version = object.getString("version");
Log.d("TAG", "id is "+id);
Log.d("TAG", "name is "+name);
Log.d("TAG", "version is "+version);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
...
}
2.使用GSON
添加GSON依赖:
implementation 'com.google.code.gson:gson:2.8.7'
新建一个JavaBean
public class App {
private String id;
private String name;
private String version;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
}
修改MainActivity代码
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
...
private void sendRequestWithOkHttp() {
new Thread(new Runnable() {
@Override
public void run() {
try {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("http://10.0.2.2/get_data.json")
.build();
Response response = client.newCall(request).execute();
String s = response.body().string();
parseJSONWithGson(s);
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
private void parseJSONWithGson(String dataJSON) {
Gson gson = new Gson();
List<App> appList = gson.fromJson(dataJSON, new TypeToken<List<App>>() {}.getType());
for(App app:appList){
Log.d("TAG", "id is :"+app.getId());
Log.d("TAG", "name is :"+app.getName());
Log.d("TAG", "version is :"+app.getVersion());
}
}
...
}
完!