文章目錄
- 11 網路編程
- 11.1 通過HTTP訪問網路
- 11.1.1 HTTP協議通信簡介
- 11.1.2 使用HttpURLConnection訪問網路
- 11.2 使用WebView進行網路開發
- 11.2.1 使用WebVIew瀏覽網頁
- 11.2.2 使用 WebView 執行HTML代碼
- 11.2.3 設定WebView支持JavaScript
- 11.3 JSON資料決議
- 11.3.1 JSON資料
- 11.3.2 JSON決議(兩種決議方式)
- 11.3.3 實戰演練--天氣預報
11 網路編程
11.1 通過HTTP訪問網路
11.1.1 HTTP協議通信簡介
? 在日常中,大多數人會使用手機進行百度搜索,這個訪問百度的程序就是通過HTTP協議完成的,所謂的HTTP(Hyper Text Transfer Protocol )即超文本傳輸協議,它規定了瀏覽器和服務器之間互相通信的規則,
? HTTP是一種請求/回應式的協議
- 當客戶端在服務器端建立連接后,向服務器發送的請求,被稱為HTTP請求,
- 服務器端請求就收到請求后做出回應,稱作HTTP回應,

11.1.2 使用HttpURLConnection訪問網路
? APP與服務器進行資料互動,也就是訪問網路,使用HttpURLConnection

- URL的構造方法中傳入要訪問資源的路徑
- setRequestMethod方法是設定請求方式
- setConnectTimeout設定超時時間
- getInputStream獲取服務器回傳的輸入流

1.GET方式請求資料


- getResponseCode獲取到狀態碼
- 狀態碼為200,表示訪問成功獲取回傳內容的輸入流
2.POST方式提交資料

- setDoOutput設定允許向外寫入資料
- getOutputStream利用輸入流向服務器寫資料
- write(data.getBytes())將資料寫給服務器
注意事項

11.2 使用WebView進行網路開發
11.2.1 使用WebVIew瀏覽網頁


WebView控制元件的常用方法
| 方法名稱 | 功能描述 |
|---|---|
| loadUrl(String url) | 用于加載指定URL對應的網頁 |
| loadData(String data,String mimeType,String encoding) | 用于將指定的字串資料加載到瀏覽器中 |
| loadDataWithBaseURL(String baseUrl,String data,String mimeType,String encoding,String historyUrl) | 基于URL加載指定的資料 |
| capturePicture() | 用于創建當前螢屏的快照 |
| goBack() | 執行后退操作,相當于瀏覽器上后退按鈕的功能 |
| goForward() | 執行前進操作,相當于瀏覽器上前進按鈕的功能 |
| stopLoading() | 停止加載當前頁面 |
| reload() | 重繪當前頁面 |
注意:
如果想讓上述WebView控制元件具備放大和縮小網頁的功能,則需要對該控制元件進行如下設定:
webView.getSettings().setSupportZoom(true);
webView.getSettings().setBuiltInZoomControls(true);
11.2.2 使用 WebView 執行HTML代碼

- baseUrl:指定當前頁面使用的基本URL,如果為null,則使用默認的about:blank,及空白頁
- data:要顯示的字串資料
- mimeType:顯示內容為MIME型別,如果為null,則默認使用text/html
- encoding:指定資料的編碼方式
- historyUrl:進入該頁面前顯示頁的URL,也就是進入該頁面顯示頁的URL,如果null,則默認使用about:blank
例:
package com.example.administrator.text_11;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
public class WebViewHtml extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view_html);
WebView webView = findViewById(R.id.webView2);
//創建一個字串構造器,將要顯示的HTML內容放置在該構造器中
StringBuilder sb = new StringBuilder();
sb.append("<div>請選擇您要學習的課程:</div>");
sb.append("<ul>");
sb.append("<li>新媒體課程</li>");
sb.append("<li>大資料課程</li>");
sb.append("<li>人工智能課程</li>");
sb.append("</ul>");
//加載資料
webView.loadDataWithBaseURL(null,sb.toString(),"text/html","utf-8",null);
}
}
11.2.3 設定WebView支持JavaScript

11.3 JSON資料決議
11.3.1 JSON資料

1.物件結構

2.資料結構


11.3.2 JSON決議(兩種決議方式)
1.使用JSONObject與JSONArray類決議JSON資料
org.json包:該包存放了決議JSON資料的類,其中JSONObject用于決議物件結構的JSON資料,JSONArray用于決議陣列結構的JSON資料


2.使用Gson庫決議JSON資料
? 在使用Gson庫之前,需要將json.jar添加到專案中,并創建JSON資料對應的物體類,需要注意的是,物體類中的成員名稱需要和JSON資料中key值一致,

11.3.3 實戰演練–天氣預報

物體類:WeatherInfo

JSONParse:


xml檔案:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:background="@drawable/bg">
<TextView
android:id="@+id/tv_city"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="35dp"
android:layout_marginTop="40dp"
android:textSize="50sp" />
<ImageView
android:id="@+id/iv_icon"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_below="@id/tv_city"
android:layout_alignLeft="@id/tv_city"
android:layout_marginLeft="50dp"
android:layout_marginTop="40dp" />
<TextView
android:id="@+id/tv_weather"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/iv_icon"
android:layout_alignLeft="@+id/iv_icon"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"
android:gravity="center"
android:textSize="18sp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/iv_icon"
android:layout_marginLeft="25dp"
android:layout_toRightOf="@id/iv_icon"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/tv_temp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textSize="22sp" />
<TextView
android:id="@+id/tv_wind"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp" />
<TextView
android:id="@+id/tv_pm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:orientation="horizontal">
<Button
android:id="@+id/btn_bj"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="北京" />
<Button
android:id="@+id/btn_sh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="上海" />
<Button
android:id="@+id/btn_gz"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="廣州" />
</LinearLayout>
</RelativeLayout>
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/377145.html
標籤:其他
上一篇:DRouter的底層實作淺析
