黎活明8天快速掌握android视频教程--24_网络通信之网页源码查看器
1 该项目的主要功能就是从将后台的html网页在Android的界面上显示出来
后台就是建立一个java web工程在工程尚建立一个html或者jsp文件就可以了,这里主要看Android客户端的程序
xml文件:
<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:orientation="vertical" tools:context="application.weiyuan.com.lihuoming_24.MainActivity"> <TextView android:textSize="25sp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="从网页获得html源码的显示" /> <Button android:id="@+id/btn_main_download" android:textSize="25sp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="点击下载"/> <ScrollView android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/tv_main_html" android:textSize="25sp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </ScrollView> </LinearLayout>
上面需要注意的是采用ScrollView包裹TextView,因为后台返回的html的内容很多,可以让TextView滚动起来
业务类的操作代码是:
public class HtmlBussiess { public static String getHtml(String path) throws Exception { URL url = new URL(path); HttpURLConnection openConnection = (HttpURLConnection) url.openConnection(); openConnection.setConnectTimeout(5000); openConnection.setRequestMethod("GET"); //采用get的请求方式 openConnection.connect(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); InputStream inputStream = null; if(openConnection.getResponseCode() == 200){ inputStream = openConnection.getInputStream(); byte[] buffer = new byte[1024]; int len = -1; while ((len = inputStream.read(buffer)) != -1){ outputStream.write(buffer,0,len); } } inputStream.close(); return new String(outputStream.toByteArray(),"utf-8");//这里的编码方式必须和后台的html编码方式一样 } }
activity控制层的代码是:
public class MainActivity extends Activity { private TextView tv_main_html; private Button btn_main_download; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initListener(); } private void initListener() { btn_main_download.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { ExecutorService executorService = Executors.newCachedThreadPool(); executorService.execute(new Runnable() { @Override public void run() { String path = "http://192.168.1.103:8080/lihuoming_23/hello.jsp";//myeclpise建立的工程 try { final String html = HtmlBussiess.getHtml(path); runOnUiThread(new Runnable() { @Override public void run() { tv_main_html.setText(html); } }); } catch (final Exception e) { e.printStackTrace(); runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(MainActivity.this, "图片下载失败" + e.toString(), Toast.LENGTH_LONG).show(); } }); ; } } }); } }); } private void initView() { tv_main_html = (TextView) findViewById(R.id.tv_main_html); btn_main_download = (Button) findViewById(R.id.btn_main_download); } }
posted on 2017-04-24 10:20 luzhouxiaoshuai 阅读(157) 评论(0) 编辑 收藏 举报