《第一行代码》读书笔记(十)----网络编程

WebView 的用法

新建一个项目, 修改 activity_main.xml, 添加一个 WebView :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <WebView
        android:id="@+id/web_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</RelativeLayout>

修改 MainActivity :

public class MainActivity extends Activity {

    private WebView webView;

    @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(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);//根据传入的参数再去加载新的网页
                return true;//表示当前 WebView 可以处理打开新网页的请求, 不用借助系统浏览器
            }
        });

        webView.loadUrl("http://www.baidu.com");
    }
}

最后, 由于使用了网络, 申请权限:

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

使用 HTTP 协议访问网络

HTTP 协议工作原理:
客户端向服务器发送一条 HTTP 请求, 服务器收到请求后返回一些数据给客户端, 客户端再对这些数据进行解析和处理.
浏览器工作流程: 发送 HTTP 请求–> 接受服务响应–> 解析返回数据–> 页面展示

使用 HttpURLConnection

流程:
获取到 HttpURLConnection 实例–> 设置 HTTP 请求方法–> 自由设置(连接超时读取超时等)–> 获取到返回的输入流–> 关闭连接

新建一个项目, 修改 activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

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

注: <ScrollView>, 允许以滚动的形式查看屏幕外的内容.

修改 MainActivity:

public class MainActivity extends Activity implements View.OnClickListener {

    public static final int SHOW_RESPONSE = 0;

    private Button sendRequest;
    private TextView responseText;

    private Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case SHOW_RESPONSE:
                    String response = (String) msg.obj;
                    responseText.setText(response);
            }
        }
    };

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

        sendRequest = (Button) findViewById(R.id.send_request);
        responseText = (TextView) findViewById(R.id.response_text);

        sendRequest.setOnClickListener(this);
    }

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

    private void sendRequestWithHttpURLConnection() {
        //开启线程发起网络请求
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpURLConnection connection = null;
                try {
                    URL url = new URL("http://www.baidu.com");
                    connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("GET");
                    connection.setConnectTimeout(8000);
                    connection.setReadTimeout(8000);
                    InputStream in = connection.getInputStream();

                    //读取输入流
                    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                    StringBuilder response = new StringBuilder();
                    String line;
                    while ((line = reader.readLine()) != null) {
                        response.append(line);
                    }

                    Message message = new Message();
                    message.what = SHOW_RESPONSE;
                    message.obj = response.toString();
                    handler.sendMessage(message);
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (connection != null) {
                        connection.disconnect();
                    }
                }
            }
        }).start();
    }
}

最后添加权限.

运行程序, 可以看到返回的是 HTML 代码.

使用 HttpClient

在刚才创建的项目中的 MainActivity 类中添加一个方法:

private void sendRequestWithHttpClient() {
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                HttpClient httpClient = new DefaultHttpClient();
                HttpGet httpGet = new HttpGet("http://www.baidu.com");
                HttpResponse httpResponse = httpClient.execute(httpGet);

                if (httpResponse.getStatusLine().getStatusCode() == 200) {
                    HttpEntity entity = httpResponse.getEntity();
                    String response = EntityUtils.toString(entity, "utf-8");//中文防乱码

                    Message message = new Message();
                    message.what = SHOW_RESPONSE;
                    message.obj = response.toString();
                    handler.sendMessage(message);
                }
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }).start();
}

解析 XML 格式数据

使用 tomcat 服务器. 自己创建一个 get_data.xml文件, 内容:

<apps>
    <app>
        <id>1</id>
        <name>Google Map</name>
        <version>1.0</version>
    </app>
    <app>
        <id>2</id>
        <name>Chrome</name>
        <version>2.1</version>
    </app>
    <app>
        <id>3</id>
        <name>Google Play</name>
        <version>2.3</version>
    </app>
</apps>

Pull 解析方式

修改 MainActivity :

public class MainActivity extends Activity implements View.OnClickListener {

    ...

    private void sendRequestWithHttpClient() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    HttpClient httpClient = new DefaultHttpClient();

                    //指定访问的服务器地址是电脑本机(10.0.2.2对于模拟器来说就是电脑本机的IP 地址)
                    HttpGet httpGet = new HttpGet("http://10.0.2.2/get_data.xml");

                    HttpResponse httpResponse = httpClient.execute(httpGet);

                    if (httpResponse.getStatusLine().getStatusCode() == 200) {
                        HttpEntity entity = httpResponse.getEntity();
                        String response = EntityUtils.toString(entity, "utf-8");//中文防乱码

                        //开始进行 Pull 解析
                        parseXMLWithPull(response);
                    }
                } catch (ClientProtocolException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    //Pull 解析的具体流程
    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("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();
        }
    }
    ...
}

注意, 把tomcat的端口号修改为80, 才能正常打印出结果.

SAX 解析方式

新建一个类继承自 DefaultHandler:

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)) {
            id.append(ch, start, length);
        }
        else if ("version".equals(nodeName)) {
            id.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());

            //将 StringBuilder 清空
            id.setLength(0);
            name.setLength(0);
            version.setLength(0);
        }
    }

    @Override
    public void endDocument() throws SAXException {
    }
}

修改 MainActivity :

public class MainActivity extends Activity implements View.OnClickListener {

    ...

    private void sendRequestWithHttpClient() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    HttpClient httpClient = new DefaultHttpClient();

                    //指定访问的服务器地址是电脑本机(10.0.2.2对于模拟器来说就是电脑本机的IP 地址)
                    HttpGet httpGet = new HttpGet("http://10.0.2.2/get_data.xml");

                    HttpResponse httpResponse = httpClient.execute(httpGet);

                    if (httpResponse.getStatusLine().getStatusCode() == 200) {
                        HttpEntity entity = httpResponse.getEntity();
                        String response = EntityUtils.toString(entity, "utf-8");//中文防乱码

                        //开始进行 Pull 解析
                        //parseXMLWithPull(response);

                        //开始进行 SAX 解析
                        parseXMLWithSAX(response);
                    }
                } catch (ClientProtocolException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    //SAX 解析具体流程
    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 格式数据

相比于 XML, JSON格式的主要优势在于它的体积更小, 在网络上传输的时候更省流量. 缺点在于, 它的语义性较差, 看起来不如 XML 直观.
新建一个 get_data.json :

[{"id":"5","version":"5.5","name":"Angry Birds"},
{"id":"6","version":"7.0","name":"Clash of Clans"},
{"id":"7","version":"3.5","name":"Hey Day"}]

使用 JSONObject

修改 MainActivity 的代码:

public class MainActivity extends Activity implements View.OnClickListener {

    ...
    private void sendRequestWithHttpClient() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    HttpClient httpClient = new DefaultHttpClient();

                    //指定访问的服务器地址是电脑本机(10.0.2.2对于模拟器来说就是电脑本机的IP 地址)
                    HttpGet httpGet = new HttpGet("http://10.0.2.2/get_data.json");

                    HttpResponse httpResponse = httpClient.execute(httpGet);

                    if (httpResponse.getStatusLine().getStatusCode() == 200) {
                        HttpEntity entity = httpResponse.getEntity();
                        String response = EntityUtils.toString(entity, "utf-8");//中文防乱码

                        //开始进行 Pull 解析
                        //parseXMLWithPull(response);

                        //开始进行 SAX 解析
                        parseXMLWithSAX(response);

                        //使用 JSONObject 解析 JSON
                        parseJSONWithJSONObject(response);
                    }
                } catch (ClientProtocolException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    //  JSONObject 解析具体流程
    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();
        }
    }

    ...
}

使用 GSON

新建一个 App 类:

public class App {

    private String id;
    private String name;
    private String version;

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getVersion() {
        return 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;
    }
}

修改 MainActivity 的代码:

public class MainActivity extends Activity implements View.OnClickListener {

    ...

    private void sendRequestWithHttpClient() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    HttpClient httpClient = new DefaultHttpClient();

                    //指定访问的服务器地址是电脑本机(10.0.2.2对于模拟器来说就是电脑本机的IP 地址)
                    HttpGet httpGet = new HttpGet("http://10.0.2.2/get_data.json");

                    HttpResponse httpResponse = httpClient.execute(httpGet);

                    if (httpResponse.getStatusLine().getStatusCode() == 200) {
                        HttpEntity entity = httpResponse.getEntity();
                        String response = EntityUtils.toString(entity, "utf-8");//中文防乱码

                        //开始进行 Pull 解析
                        //parseXMLWithPull(response);

                        //开始进行 SAX 解析
                        //parseXMLWithSAX(response);

                        //使用 JSONObject 解析 JSON
                        //parseJSONWithJSONObject(response);

                        //使用 GSON 解析 JSON
                        parseJSONWithGSON(response);
                    }
                } catch (ClientProtocolException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    //使用 GSON 解析 JSON
    private void parseJSONWithGSON(String jsonData) {
        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());
        }
    }

    ...

网络编程的最佳实践

应该将通用的网络操作提取到一个公共类里, 并提供一个静态方法, 当想要发起网络请求的时候调用一下这个方法就可以.
由于请求网络属于耗时操作, 需要在子线程中进行, 但是子线程无法通过 return 语句来返回数据, 所以使用回调机制来解决这个问题.

首先定义一个接口, 比如命名为 HttpCallbackListener :

public interface HttpCallbackListener {

    void onFinish(String response);

    void onError(Exception e);
}

该接口定义两个方法, onFinish()在请求成功时候调用, 其参数代表服务器返回的数据. onError()在请求失败时候调用, 其参数记录着错误的详细信息.

然后新建一个类, HttpUtil:

public class HttpUtil {

    public static void sendHttpRequest(final String address, final HttpCallbackListener listener) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpURLConnection connection = null;
                try {
                    URL url = new URL(address);
                    connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("GET");
                    connection.setConnectTimeout(8000);
                    connection.setReadTimeout(8000);
                    connection.setDoInput(true);
                    connection.setDoOutput(true);
                    InputStream in = connection.getInputStream();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                    StringBuilder response = new StringBuilder();
                    String line;
                    while ((line = reader.readLine()) != null) {
                        response.append(line);
                    }
                    if (listener != null) {
                        //回调 onFinish() 方法
                        listener.onFinish(response.toString());                        
                    }
                } catch (Exception e) {
                    if (listener != null) {
                        //回调 onError() 方法
                        listener.onError(e);
                    }
                } finally {
                    if (connection != null) {
                        connection.disconnect();
                    }
                }
            }
        }).start();
    }
}

在调用这个类的静态方法时, 只需:

String address = "http://www.baidu.com";
HttpUtil.sendHttpRequest(address, new HttpCallbackListener() {
    @Override
    public void onFinish(String response) {
        //在这里柑橘返回内容执行具体逻辑
    }

    @Override
    public void onError(Exception e) {
        //在这里对异常进行处理
    }
});
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值