lovejobs

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

1.android的API提供了访问网络的一个类HttpURLConnection

2.通过发送GET请求获取服务器返回的html代码

3.先看看布局文件,如下所示,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <EditText
        android:id="@+id/et"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:onClick="get"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="获取源码"/>
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:id="@+id/tv"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
           android:textColor="#00f"
            android:hint="源码显示"/>
    </ScrollView>

</LinearLayout>

布局样子:

4.下来是清单文件,记得连接网络要添加权限

5.1再看java代码,MainActivity的

package com.market.source;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLConnection;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends Activity {

    @BindView(R.id.et)
     EditText et;

    @BindView(R.id.tv)
     TextView tv;


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


    public void get(View vew) throws IOException {
        final String str = et.getText().toString().trim();


        new Thread(){

            @Override
            public void run() {
                //1.请求地址url
                URL url = null;
                try {
                    url = new URL(str);
                    //2.获取对这个地址的连接
                    HttpURLConnection con = (HttpURLConnection) url.openConnection();

                    //3.设置对这个地址的请求,GET请求
                    con.setRequestMethod("GET");
                    //4.设置请求参数
                    con.setConnectTimeout(5000);
                    //5.获取服务器的响应
                    int code = con.getResponseCode();
                    //6.根据响应吗,判断请求成功还是失败,200成功
                    if(code==200){
                        //7.成功的话,服务器一流的形式返回数据
                        InputStream inputStream = con.getInputStream();

                        //8.这个流是字节流,需要我们转换为字符流才可以认识
                        final String info = StreamTool.StreamtoString(inputStream);
                        Log.e("MainActivity",info);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                tv.setText(info);
                            }
                        });

                    }
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (ProtocolException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }


            }




        }.start();
    }


}

5.2工具类,用来将一个流转化为字符串

public class StreamTool {

    public static String StreamtoString(InputStream inputStream) {

        int len = -1;
        byte[] buffer = new byte[1024];
        //内存数组输出流
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        try {
            while((len=inputStream.read(buffer)) != -1){

               baos.write(buffer,0,len);
            }
            String str = new String(baos.toByteArray());
            return str;

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return null;
    }

}

 

6.运行效果查看

 

 

 

posted on 2017-06-23 01:01  lovejobs  阅读(1288)  评论(0编辑  收藏  举报