访问网络文本
来源:互联网 发布:未注册域名查询 编辑:程序博客网 时间:2024/05/22 12:29
package com.itheima.text;import java.io.ByteArrayOutputStream;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.URL;public class NetService {public String getText(String path) throws Exception {// 根据path访问网络, 得到服务器写回的文本URL url = new URL(path);// 把字符串路径封装成URL对象HttpURLConnection conn = (HttpURLConnection) url.openConnection();// 打开一个连接对象int code = conn.getResponseCode();// 连接服务器, 得到相应吗if (code == 200) {// 判断请求是否成功InputStream in = conn.getInputStream();// 获取输入流ByteArrayOutputStream out = new ByteArrayOutputStream();byte[] buffer = new byte[8192];int length;while((length = in.read(buffer)) != -1) // 从网络读取数据out.write(buffer, 0, length);// 把数据写到内存in.close();out.close();byte[] data = out.toByteArray();// 获取内存中的所有数据String text = new String(data, "UTF-8");// 把字节数据解码为字符串return text;}throw new RuntimeException("访问网络出错: " + code);}}
package com.itheima.text;import android.app.Activity;import android.app.ProgressDialog;import android.os.Bundle;import android.os.Handler;import android.view.View;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity {private EditText pathET;private NetService service;private TextView textTV;private Handler handler = new Handler();// 处理器, 可以在线程之间通信@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);textTV = (TextView) findViewById(R.id.textTV);pathET = (EditText) findViewById(R.id.pathET);service = new NetService();}public void go(View v) {final ProgressDialog dialog = new ProgressDialog(this);dialog.setCancelable(false);// 设置进度条不能被取消dialog.show();// 显示进度条new Thread(){public void run() {try {String path = pathET.getText().toString();// 获取地址final String text = service.getText(path);// 访问网络(4.0以上不能主线程联网), 得到文本handler.post(new Runnable(){// 向创建该handler的线程(main)发送一个Runnable, 主线程执行run()public void run() {dialog.dismiss();// 取消textTV.setText(text);// 把文本放到TextView中(主线程创建的视图, 只能在主线程修改)}});} catch (Exception e) {e.printStackTrace();Toast.makeText(getApplicationContext(), "服务器忙!", Toast.LENGTH_SHORT).show();}}}.start();}}
布局--
<RelativeLayout 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" > <Button android:id="@+id/goBT" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:onClick="go" android:text="Go" /> <EditText android:id="@+id/pathET" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignBaseline="@id/goBT" android:layout_toLeftOf="@id/goBT" android:hint="请输入要访问的地址" android:inputType="textUri" android:text="http://192.168.1.228:8080/05.Web/text.txt" /> <ScrollView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/pathET" > <TextView android:id="@+id/textTV" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" android:textSize="20sp" /> </ScrollView></RelativeLayout><uses-permission android:name="android.permission.INTERNET"/>
- 访问网络文本
- ADO访问文本数据文件
- 网络访问
- 网络文本函数(三)
- 从网络文本图片
- C# 控制文本并发访问
- 文本分析--NLTK访问文件
- 网络文本函数(二).txt
- 利用AsyncHttpClient获取网络文本
- 网络传输文本数据格式JSON
- 网络通信之获取网络文本
- 多路访问网络
- 取消网络访问权限
- 允许网络访问PostgreSQL
- 网络访问没有权限
- ubuntu 访问网络共享
- 网络访问攻防战
- 访问网络打印机
- struts2.3+spring3.2+hibernate4.2例子
- Android改变系统自带环形ProgressBar的大小
- Oracle表空间
- hdu 4715 Difference Between Primes 2013年ICPC热身赛A题 素数水题
- 开源点评:ZeroMQ简介
- 访问网络文本
- Unix/Linux C++应用开发-C++控制结构"循环语句"
- lua的注册表,_ENV,_G
- POJ 1922到学校的最少时间
- Servlet线程安全性解析
- 小小菜鸟的公告
- Unix/Linux C++应用开发-C++控制结构"控制结构跳转语句"
- 后缀自动机合集 I
- 10.linux 文件与文件系统的压缩与打包