Android 写xml
16lz
2021-01-23
package com.xh.tx.html;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import android.app.Activity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity {TextView tv_html_content = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);tv_html_content = (TextView) findViewById(R.id.tv_html_content);loadHtml("http://www.baidu.com");}//加载htmlpublic void loadHtml(String uri){try {final URL url = new URL(uri);new Thread(new Runnable() {@Overridepublic void run() {HttpURLConnection conn = null;try {conn = (HttpURLConnection) url.openConnection();conn.setConnectTimeout(30000);conn.setReadTimeout(30000);conn.setRequestMethod("GET");//以get的方式去请求数据,如果没有这句话,默认就是以get方式请求conn.connect(); //连接/** * 网络连接是否一定成功? 》》 否定的 * 如何判断网络一定成功 >> 在http请求中有一个状态来标示网络是否成功 */if(conn.getResponseCode() == 200){final String content = releaseInputStream(conn.getInputStream());//非要打印一句话runOnUiThread(new Runnable() {@Overridepublic void run() {if(null == content){Toast.makeText(MainActivity.this, "加载网页源代码失败", 0).show();}else{tv_html_content.setText(content);}}});}} catch (MalformedURLException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally{if(null != conn){conn.disconnect();}}}}).start();} catch (MalformedURLException e1) {e1.printStackTrace();}}public String releaseInputStream(InputStream in) throws IOException{byte[] buffer = new byte[1024];ByteArrayOutputStream bytearray = new ByteArrayOutputStream();int len = 0;if(null == in){return null;}else{//正常解析while((len = in.read(buffer, 0, 1024)) != -1){bytearray.write(buffer);}String content = bytearray.toString();if(content.indexOf("GBK") != -1 || content.indexOf("gbk") != -1){//content = new String(content.getBytes(),"gbk");//中国 》》 01 10 //只针对网页 解决乱码问题/** * 总结乱码问题: * 以后做项目统一几个编码 * 1. 项目的编码 * 2. 网络编码 * 3. 获取的时候流的编码 * 4. 数据库的编码 */content = new String(bytearray.toByteArray(),"GBK");}bytearray.close();return content;}}}
更多相关文章
- android异步发网络请求
- android 网络判断
- android 网络视频代码
- Android 获取网络图片
- Android 图片加载缓存
- 网络时间同步
- Android 如何加载大图片
- Android 图片加载库Glide
- Android第五期 - 更新自己的apk本地与网络两种方法