安卓学习笔记(九)网络编程

总结《第一行代码》Android学习笔记(九)网络编程

  • 网络编程
    • WebView
    • 使用Http协议访问网络
      • 使用HttpURLConnection
      • 使用OKHttp
    • 解析XML格式数据
      • Pull解析方式
      • SAX解析方式
    • 解析JSON格式数据
      • 使用JSONObject
      • 使用GSON

网络编程


WebView

在布局文件中添加一个WebView,然后修改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支持JavaScript脚本
        webView.setWebViewClient(new WebViewClient());//在WebView上显示网页
        webView.loadUrl("http://www.baidu.com");
    }
}

在AndroidManifest中添加网络权限声明和为Application添加usesCleartextTraffic属性:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.youyu4.webviewtest">
    <uses-permission android:name="android.permission.INTERNET">uses-permission>

    <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"
        android:usesCleartextTraffic="true">
        <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>

注意只能使用Android版本API28及以下的模拟器来运行该项目,API28以上的模拟器无法正常在WebView中显示网页!


使用Http协议访问网络

新建项目NetworkTest,布局文件中包含一个按钮和ScrollView,ScrollView中包含一个TextView用于显示http请求:


<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>

注意要在AndroidManifest中添加网络权限声明和为Application添加usesCleartextTraffic属性。

使用HttpURLConnection

MainActivity:

public class MainActivity extends AppCompatActivity {

    TextView responseText;

    private static final String TAG = "MainActivity";

    @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(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                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 in = connection.getInputStream();
                    //下面对获取到的输入流进行读取
                    reader = new BufferedReader(new InputStreamReader(in));
                    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 response){
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                //在这里进行UI操作,将结果显示到界面上
                responseText.setText(response);
            }
        });
    }
}

使用OKHttp

首先先在build.gradle中添加依赖库:

implementation 'com.squareup.okhttp3:okhttp:4.0.1'

然后在MainActivity添加方法:

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 responseData = response.body().string();
                showResponse(responseData);
                //parseXMLWithPull(responseData);//Pull解析XML
                //parseXMLWithSAX(responseData);//SAX解析XML
                //parseJSONWithJSONObject(responseData);//JSONObject解析JSON
                //parseJSONWithGSON(responseData);//GSON解析JSON
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }).start();
}

解析XML格式数据

首先,建立一个Apache服务器,然后在服务器上加入XML格式的内容:

<apps>
	<app>
		<id>1id>
		<name>Google Mapsname>
		<version>1.0version>
	app>
	<app>
		<id>2id>
		<name>Chromename>
		<version>2.1version>
	app>
	<app>
		<id>3id>
		<name>Google Playname>
		<version>2.3version>
	app>
apps>

Pull解析方式

Pull解析方式的主要原理是先获取一个XmlPullParserFactory实例,然后借助这个实例获取XmlPullParser对象,然后调用XmlPullParser对象的setInput()方法将服务器返回的XML数据设置进去,然后在while循环中解析。

在MainActivity中添加方法:

//使用Pull解析XML
private void parseXMLWithPull(String xmlData){
    try {
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        XmlPullParser xmlPullParser = factory.newPullParser();
        xmlPullParser.setInput(new StringReader(xmlData));
        int eventType = xmlPullParser.getEventType();
        String id = "";
        String name = "";
        String version = "";
        while(eventType != XmlPullParser.END_DOCUMENT){
            String nodeName = xmlPullParser.getName();
            switch (eventType){
                //开始解析某个节点
                case XmlPullParser.START_TAG:{
                    if("id".equals(nodeName)){
                        id = xmlPullParser.nextText();
                    }else if("name".equals(nodeName)){
                        name = xmlPullParser.nextText();
                    }else if("version".equals(nodeName)){
                        version = xmlPullParser.nextText();
                    }
                    break;
                }
                //完成解析某个节点
                case XmlPullParser.END_TAG:{
                    if("app".equals(nodeName)){
                        Log.d("aaa", "id is " + id);
                        Log.d("aaa", "name is " + name);
                        Log.d("aaa", "version is " + version);
                    }
                    break;
                }
                default:
                    break;
            }
            eventType = xmlPullParser.next();
        }
    }catch(Exception e){
        e.printStackTrace();
    }
}

SAX解析方式

SAX解析方式的主要原理是重写DefaultHandler中的方法来使解析XML时到的不同节点时要做的逻辑,然后创建SAXParserFactory对象,获取XMLReader对象,然后将ContentHandler实例设置到XMLReader中,最后调用parse()方法开始执行解析。

新建ContentHandler继承自DefaultHandler,并重写父类5个方法:

public class ContentHandler extends DefaultHandler {

    private String nodeName;

    private StringBuilder id;

    private StringBuilder name;

    private StringBuilder version;

    @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;
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        //根据当前的节点名判断将内容添加到哪一个StringBuilder对象中
        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)){
            Log.d("aaa", "id is " + id.toString().trim());
            Log.d("aaa", "name is " + name.toString().trim());
            Log.d("aaa", "version is " + version.toString().trim());
            //最后要将StringBuilder清空掉
            id.setLength(0);
            name.setLength(0);
            version.setLength(0);
        }
    }

    @Override
    public void endDocument() throws SAXException {
        super.endDocument();
    }
}

在MainActivity中添加方法:

//使用SAX解析XML
private void parseXMLWithSAX(String xmlData){
    try{
        SAXParserFactory factory = SAXParserFactory.newInstance();
        XMLReader xmlReader = factory.newSAXParser().getXMLReader();
        ContentHandler handler = new ContentHandler();
        //将ContentHandler的实例设置到XMLReader中
        xmlReader.setContentHandler(handler);
        //开始执行解析
        xmlReader.parse(new InputSource(new StringReader(xmlData)));
    }catch (Exception e){
        e.printStackTrace();
    }
}

解析JSON格式数据

在Apache服务器上加入JSON格式的内容:

[{"id":"5","version":"5.5","name":"Clash of Clans"},
{"id":"6","version":"7.0","name":"Boom Beach"},
{"id":"7","version":"3.5","name":"Clash Royale"}]

使用JSONObject

使用JSONObject解析JSON的主要原理是将服务器返回的数据传入一个JSONArray中,然后遍历这个JSONArray并取出JSONObject对象,每个JSONObject对象中又包含了数据,最后只需调用getString方法将这些数据取出。

在MainActivity中添加方法:

//使用JSONObject解析JSON
private void parseJSONWithJSONObject(String jsonData){
    try{
        JSONArray jsonArray = new JSONArray(jsonData);
        for(int i = 0; i < jsonArray.length(); i++){
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            String id = jsonObject.getString("id");
            String name = jsonObject.getString("name");
            String version = jsonObject.getString("version");
            Log.i("aaa", "id is " + id);
            Log.i("aaa", "name is " + name);
            Log.i("aaa", "version is " + version);
        }
    }catch (Exception e){
        e.printStackTrace();
    }
}

使用GSON

使用GSON解析JSON的主要原理是GSON库能将一段JSON格式的字符串自动映射成一个对象。

首先,在build.gradle中添加依赖库:

implementation 'com.google.code.gson:gson:2.8.5'

新建App类:

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中添加方法:

//使用GSON解析JSSO
private void parseJSONWithGSON(String jsonData){
    Gson gson = new Gson();
    List<App> appList = gson.fromJson(jsonData,new TypeToken<List<App>>(){}.getType());//解析JSON数据成App对象
    for(App app : appList){
        Log.i("aaa", "id is " + app.getId());
        Log.i("aaa", "name is " + app.getName());
        Log.i("aaa", "version is " + app.getVersion());
    }
}

你可能感兴趣的:(安卓学习笔记)