Android学习笔记-使用网络技术

使用网络技术

  本章主要讲述如何在手机端使用HTTP和服务器进行网络交互,并对服务器返回的数据进行解析,这也是Android中最常使用到的网络技术

01 WebView的用法

  WebView控件,借助它我们就可以在自己的应用程序里嵌入一个浏览器,从而非常轻松地展示各种各样的网页。

//需在manifest.xml文件中添加
//application属性:android:usesCleartextTraffic="true"
//权限<uses-permission android:name="android.permission.INTERNET" />.
//使用百度进行搜索后会有net:err_unknown_url_scheme错误
WebView webView = (WebView) findViewById(R.id.webView);
//让其支持JavaScript脚本
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("http://www.baidu.com");
权限允许
<uses-permission android:name="android.permission.INTERNET" />
在manifest.xml中添加application的属性
<application
    android:usesCleartextTraffic="true"
/>

02 使用HTTP协议访问网络

  工作原理特别简单,就是客户端向服务器发出一条HTTP请求,服务器收到请求之后会返回一些数据给客户端,然后客户端再对这些数据进行解析和处理.

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    TextView responseText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //发送http请求
        Button sendHttp = (Button) findViewById(R.id.sendHttp);
        sendHttp.setOnClickListener(this);

        responseText = (TextView) findViewById(R.id.responseText);


    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.sendHttp){
            sendRequestWithHttpURLConnection();
        }
    }

    private void sendRequestWithHttpURLConnection() {
        //1 开启线程发起网络请求
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpURLConnection connection = null;
                BufferedReader reader = null;

                try {
                    //2 获取链接
                    URL url = new URL("https://www.baidu.com");
                    connection = (HttpURLConnection) url.openConnection();
                    //3 链接设置
                    connection.setRequestMethod("GET");
                    connection.setConnectTimeout(8000);
                    connection.setReadTimeout(8000);
                    //4 对获取到的输入流进行读取
                    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) {
        //5 通过runOnUiThread在主线程进行UI操作
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                //在这里进行UI操作
                responseText.setText(response);
            }
        });
    }
}

权限

<uses-permission android:name="android.permission.INTERNET" />

03 使用OkHttp

导入依赖

implementation 'com.squareup.okhttp3:okhttp:4.1.0'
public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    TextView responseText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //发送http请求
        Button sendHttp = (Button) findViewById(R.id.sendHttp);
        sendHttp.setOnClickListener(this);

        responseText = (TextView) findViewById(R.id.responseText);


    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.sendHttp){
            sendRequestWithOkHttp();
        }
    }

    private void sendRequestWithOkHttp() {
        //1 开启线程发起网络请求
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    //2 获得链接
                    OkHttpClient client = new OkHttpClient();
                    Request request = new Request.Builder().
                            url("http://www.baidu.com")
                            .build();
                    //3 接收结果
                    Response response = client.newCall(request).execute();
                    String responseData = response.body().string();
                    showResponse(responseData);
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }).start();

    }

    private void showResponse(final String response) {
        //4 通过runOnUiThread在主线程进行UI操作
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                //在这里进行UI操作
                responseText.setText(response);
            }
        });
    }
}

04 解析XML数据-PULL

Ubuntu下安装apche

sudo apt-get install apache2  //其他配置自行设置

在var/www/html下建立get_data.xml

<apps>
	<app>
		<id>1</id>
		<name>google maps</name>
		<version>1.0</version>
	</app>
</apps>

新建xml/network_config.xml让程序允许http

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

manifest中application属性添加

android:networkSecurityConfig="@xml/network_config
private void sendRequestWithOkHttp() {
    //1 开启线程发起网络请求
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                //2 获得链接
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder().
                        url("http://10.92.36.25/get_data.xml") //网址10.0.2.2或者自己的ip地址
                        .build();
                //3 接收结果
                Response response = client.newCall(request).execute();
                String responseData = response.body().string();
                parseXMLWithPull(responseData);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }).start();

}

//解析xml
private void parseXMLWithPull(String xmlData) {
    //进行解析
    XmlPullParserFactory factory = null;
    try {
        //通过工厂获得XmlPullParser对象
        factory = XmlPullParserFactory.newInstance();
        XmlPullParser xmlPullParser = factory.newPullParser();
        //setInput将服务器返回的XML数据进行解析
        xmlPullParser.setInput(new StringReader(xmlData));
        //getEventType()可以得到当前解析事件,然后在一个while循环中不断解析
        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(TAG, "id is "+ id);
                        Log.d(TAG, "name is "+ name);
                        Log.d(TAG, "version is "+ version);
                    }
                    break;
                }
                default:
                    break;
            }
            eventType = xmlPullParser.next();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

05 解析XML数据-SAX

新建类继承DefaultHandler

public class ContentHandler extends DefaultHandler {

    private static final String TAG = "ContentHandler";
    private String nodeName;
    private StringBuilder id;
    private StringBuilder name;
    private StringBuilder version;

    //开始document解析时调用
    @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(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();
    }
}
MainActivity.java
private void sendRequestWithOkHttp() {
    //1 开启线程发起网络请求
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                //2 获得链接
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder().
                        url("http://10.92.36.25/get_data.xml") //网址10.0.2.2或者自己的ip地址
                        .build();
                //3 接收结果
                Response response = client.newCall(request).execute();
                String responseData = response.body().string();
                parseXMLWithSAX(responseData);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }).start();

}

//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();
    }
}

  首先给id、name和version节点分别定义了一个StringBuilder对象,并在**startDocument()**方法里对它们进行了初始化。每当开始解析某个节点的时候,**startElement()方法就会得到调用,其中localName参数记录着当前节点的名字,这里我们把它记录下来。接着在解析节点中具体内容的时候就会调用characters()方法,我们会根据当前的节点名进行判断,将解析出的内容添加到哪一个StringBuilder对象中。最后在endElement()**方法中进行判断,如果app节点已经解析完成,就打印出id、name和version的内容。需要注意的是,目前id、name和version中都可能是包括回车或换行符的,因此在打印之前我们还需要调用一下trim()方法,并且打印完成后要将StringBuilder的内容清空,不然的话会影响下一次内容的读取。

06 解析JSON数据-JSONObject

同样先在var/www/html(apache默认根目录)下创建get_data.json

private void sendRequestWithOkHttp() {
    //1 开启线程发起网络请求
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                //2 获得链接
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder().
                        url("http://10.92.36.25/get_data.json") //网址10.0.2.2或者自己的ip地址
                        .build();
                //3 接收结果
                Response response = client.newCall(request).execute();
                String responseData = response.body().string();
                parseJSONWithJSONObject(responseData);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }).start();

}

//解析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.d(TAG, "id is "+ id);
            Log.d(TAG, "name is "+ name);
            Log.d(TAG, "version is "+ version);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

07 解析JSON数据-GSON

<!--添加依赖-->
implementation 'com.google.code.gson:gson:2.8.5'
private void sendRequestWithOkHttp() {
    //1 开启线程发起网络请求
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                //2 获得链接
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder().
                        url("http://10.92.36.25/get_data.json") //网址10.0.2.2或者自己的ip地址
                        .build();
                //3 接收结果
                Response response = client.newCall(request).execute();
                String responseData = response.body().string();
                parseJSONWithGSON(responseData);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }).start();

}

//解析JSON
private void parseJSONWithGSON(String jsonData) {
    try {
        Gson gson = new Gson();
        List<App> appList = gson.fromJson(jsonData,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());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

得新建一个类用于映射

public class App {
    private String id;
    private String name;
    private String version;

    public void setId(String id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getVersion() {
        return version;
    }
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值