Android简单联网获取网页

一门语言,联网编程都是非常重要,所以我们来简单的实现以下android开发如何写联网程序。

Http请求分为:

  1. GET:明文传参,在地址栏上可以看到参数,调用简单没,不安全。
  2. POST:暗文传参,在地址栏上看不到参数,调用稍微复杂,安全。

android上发送Http请求的方式有两种:

  1. HttpURLConnection类:是java的标准指定网站发送GET请求、POST请求类,使用相对简单,并且易于扩展,推荐使用。
  2. HttpClient类:是android SDK提供的请求方式,对HTTP协议全面支持,但是在android6.0(API23)中,Google已经移除了该相关的类,因此不在赘述使用该类的使用。

    这次我们使用GET方式进行获取数据,步骤:

    1.创建URL对象。
    2.通过 URL对象调用openConnection()方法获取HttpURLConnection对象。
    3.设置相关属性。
    4.HttpURLConnection对象通过getInputStream()方法获得输入流。
    5.读取输入流,转成字符串。


效果图:
这里写图片描述


**其实很简单,列举一个例子:
假如从水库引水,首先要找到水库的位置,第二步将水引到工厂后建立一个阀门来控制水,第三步建立水道将水引到蓄水池,在水道口放上网来过滤鱼虾蟹,然后用小水桶把渔网的鱼一点一点的捞起来放到大水桶,最后晒网。**


代码演示:

布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.httpdemo.MainActivity" >
    <Button 
        android:id="@+id/a12"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="获取"
        android:onClick="aaa"
        />

    <ScrollView
        android:layout_below="@id/a12"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <TextView
            android:id="@+id/a"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_world" />
    </ScrollView>

</RelativeLayout>

MainActivity:

package com.example.httpdemo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity {
    // textview
    private TextView mTextView;
    // handler线程
    private Handler mHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            mTextView.setText((String) msg.obj);
        };
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 绑定id
        mTextView = (TextView) findViewById(R.id.a);

    }

    // 联网
    private void getHttp() {
        InputStream stream = null;
        InputStreamReader re = null;
        BufferedReader reader = null;
        try {
            // 找水源
            URL url = new URL("http://www.baidu.com");
            // 建立总闸
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            // 建立水道
            stream = conn.getInputStream();
            // 捕鱼
            // 小渔网
            re = new InputStreamReader(stream);
            // 大渔网
            reader = new BufferedReader(re);

            // 捞鱼
            // 大水桶
            String len = "";
            // 小水桶
            String temp = "";
            while ((temp = reader.readLine()) != null) {
                len += temp;
            }
            // 更新UI
            Message sg = new Message();
            sg.obj = len;
            mHandler.sendMessage(sg);

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 晒网,关闭资源
            try {
                if (reader != null) {
                    reader.close();
                }
                if (re != null) {
                    re.close();
                }
                if (stream != null) {
                    stream.close();
                }

            } catch (Exception e2) {
            }
        }
    }

    // 按钮监听
    public void aaa(View v) {
        //新建一个线程
        new Thread() {
            public void run() {
                getHttp();
            };
        }.start();
    }
}

android权限:

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

代码打包:android简单联网

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值