android 74 下载文本
2015-11-01 22:21
387 查看
页面:
JAVA:
<LinearLayout 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=".MainActivity" android:orientation="vertical" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="查看源文件" android:onClick="click" /> <ScrollView android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </ScrollView> </LinearLayout>
JAVA:
package com.itheima.htmlviewer; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import com.itheima.htmlviewer.utils.Utils; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.TextView; //请求网上的文本(网页) public class MainActivity extends Activity { Handler handler = new Handler(){ public void handleMessage(android.os.Message msg) { TextView tv = (TextView) findViewById(R.id.tv); tv.setText((String)msg.obj); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void click(View v){ Thread t = new Thread(){ @Override public void run() { String path = "http://192.168.13.13:8080/baidu.html"; try { URL url = new URL(path); //获取连接对象,此时还未建立连接 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5000); conn.setReadTimeout(5000); //先建立连接,然后获取响应码 if(conn.getResponseCode() == 200){ //拿到服务器返回的输入流,流里的数据就是html的源文件 InputStream is = conn.getInputStream(); //从流里把文本数据取出来 String text = Utils.getTextFromStream(is); //发送消息,让主线程刷新ui,显示源文件 Message msg = handler.obtainMessage(); msg.obj = text; handler.sendMessage(msg); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }; t.start(); } }
package com.itheima.htmlviewer.utils; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; public class Utils { public static String getTextFromStream(InputStream is){ //乱码是因为服务器和客户端码表不一致 byte[] b = new byte[1024]; int len = 0; //创建字节数组输出流,读取输入流的文本数据时,同步把数据写入数组输出流 ByteArrayOutputStream bos = new ByteArrayOutputStream(); try { while((len = is.read(b)) != -1){ bos.write(b, 0, len);//写入到数组输出流 } //把字节数组输出流里的数据转换成字节数组 String text = new String(bos.toByteArray(),"utf-8");//安卓默认是utf-8 return text; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } }
相关文章推荐
- 分享一个两个Android scrollView同时滚动的方法
- Android Studio 快捷键 [用到一个记下一个]
- Android应用横竖屏切换处理
- Android之线程池的使用
- 使用传统Android组件实现高效数据加载
- mars老师[Android开发视频教学].01_18 文件下载 源码
- 设计模式之工厂模式(Factory)在Android中的应用
- Android Material Design 动画实现
- Android动画进阶—使用开源动画库nineoldandroids
- 在Android Studio中创建项目和模拟器
- Android 日志打印工具类 可显示打印所在的方法和行号
- Android 实现一个立方体旋转效果
- Android 检测键盘是否显示及隐藏键盘
- Android中Intent传递对象的两种方法------Serializable传值和Parcelable传值
- Android GPS GPSBasics project hacking
- 如何解决Android studio已分享到github的项目但是git->commit directory提交不上去
- android 73 下载图片
- Android,Java延迟两秒执行
- Android之ListView的优化
- android 实现自动换行的流布局