• android:HttpURLConnection


    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
        <ScrollView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >
    
            <TextView
                android:id="@+id/textView"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />
        </ScrollView>
    
    </LinearLayout>


    package com.example.utils;
    
    import java.io.ByteArrayOutputStream;
    import java.io.InputStream;
    
    public class StreamTool {
    	public static byte[] readInputStream(InputStream inStream) throws Exception {
    		ByteArrayOutputStream outStream = new ByteArrayOutputStream();// 输出流,写到内存
    		byte[] buffer = new byte[1024];// 1K缓冲区容量
    		int len = 0;
    		while ((len = inStream.read(buffer)) != -1) {
    			outStream.write(buffer, 0, len);// 从数组buffer中读取从0到len的数据
    		}
    		inStream.close();
    		return outStream.toByteArray();
    	}
    }
    


    package com.example.image;
    
    import java.io.InputStream;
    import java.net.HttpURLConnection;
    import java.net.URL;
    
    import com.example.utils.StreamTool;
    
    public class HtmlService {
    	public static String getHtml(String path) throws Throwable {
    		URL url = new URL(path);
    		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    		conn.setRequestMethod("GET");
    		conn.setConnectTimeout(5 * 1000);
    		InputStream inStream = conn.getInputStream();// 通过输入流获取html数据
    		byte[] data = StreamTool.readInputStream(inStream);// 得到的html的二进制数据
    		String html= new String(data,"utf-8");
            return (html);
    	}
    }
    

    package com.example.image;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.widget.TextView;
    import android.widget.Toast;
    
    public class MainActivity extends Activity {
    
        private static final String TAG="htmlcodeview";
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_image);
    		TextView textView=(TextView)this.findViewById(R.id.textView);
    		try {
    			
    			textView.setText(HtmlService.getHtml("www.baidu.con"));
    		} catch (Throwable e) {
    			Log.e(TAG, e.toString());
    			Toast.makeText(MainActivity.this, "error", 1)
    			.show();
    		}
    	}
    }
    // <!-- 访问internet权限 -->
    // <uses-permission android:name="android.permission.INTERNET"/>
    
    



  • 相关阅读:
    C字符串格式化
    oms登录bug
    宏旺文章审核功能使用指引
    phpcms文章审核
    html5的pushState 无刷新, 前进后退等
    js路由—backbone的路由的实现02
    js路由—backbone的路由的实现01
    js路由—简单路由的实现
    img标签和css的background-image的区别
    querySelector与querySelectorAll
  • 原文地址:https://www.cnblogs.com/javafly/p/6037232.html
Copyright © 2020-2023  润新知