xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:orientation="vertical"
    tools:context="com.jiutouxiang.help.WebSourceViewerActivity">
    <EditText
        android:id="@+id/et_url"
        android:hint="请输入网址"
        android:textColor="#333333"
        android:textColorHint="#888888"
        android:text="http://www.baidu.com"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/bt_go"
        android:text="查看"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:textColor="#333333"
            android:id="@+id/tv_result"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </ScrollView>

</LinearLayout>

java代码:

package com.jiutouxiang.help;

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

import com.jiutouxiang.R;
import com.jiutouxiang.base.MyActivity;
import com.jiutouxiang.toolkits.ForLog;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class WebSourceViewerActivity extends MyActivity {
    private Button bt_go;
    private Activity activity;
    private EditText et_url;
    private TextView tv_result;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_source_viewer);
        activity=this;
        et_url=(EditText)activity.findViewById(R.id.et_url);
        tv_result=(TextView)activity.findViewById(R.id.tv_result);
        bt_go=(Button)activity.findViewById(R.id.bt_go);
        bt_go.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        //获取路径
                        String path=et_url.getText().toString().trim();
                        getWebSourceByPath(path);
                    }
                }).start();

            }
        });

    }
    private void getWebSourceByPath(String path){
        try {
            URL url=new URL(path);
            //拿到HttpURLConnection对象,用于接受和发送数据
            HttpURLConnection httpURLConnection= (HttpURLConnection) url.openConnection();
            //设置请求方式
            // httpURLConnection.setRequestMethod("GET");
            //设置请求超时时间:5s
            // httpURLConnection.setConnectTimeout(5000);
            //获取服务器返回响应吗
            int code=httpURLConnection.getResponseCode();
            if (code==200){
                //说明请求成功
                final InputStream in=httpURLConnection.getInputStream();
                final String result=inputStream2String(in);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                            tv_result.setText(result);
                    }
                });
            }else if(code==302){
                //拿到新地址
                String location = httpURLConnection.getHeaderField("Location");
                ForLog.showError("location"+location+"");
                //getWebSourceByPath(location);
            } else{
                ForLog.showError("返回代码错误:"+code+"---"+path);
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private String inputStream2String(InputStream in) throws IOException {
        //定义一个内存输出流
        ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
        int len=-1;
        byte[] buffer=new byte[1024];
        while((len=in.read(buffer))!=-1){
            outputStream.write(buffer,0,len);
        }
        in.close();
       return  outputStream.toString();

    }
}

results matching ""

    No results matching ""