您的位置:首页 > 理论基础 > 计算机网络

Android开发16——获取网络资源之基础应用 推荐

2012-12-12 16:48 573 查看
一、项目背景

在Android开发中有一项非常广泛的应用:Android项目获取另一个web项目的资源或者返回的数据。本博文介绍了获取另一个web项目的资源。有一个web项目,在其WebRoot文件夹下有一个静态页面test.html。现有一个Android项目要获取到该页面的html代码显示在TextView中。

二、实例代码

public class MainActivity extends Activity
{
private EditText txtPath;
private Button btnShowHtml;
private TextView txtViewHtml;

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txtPath = (EditText)this.findViewById(R.id.txtPath);
btnShowHtml = (Button)this.findViewById(R.id.btnShowHtml);
txtViewHtml = (TextView)this.findViewById(R.id.txtViewHtml);
btnShowHtml.setOnClickListener(new ShowHtmlListener());
}

private final class ShowHtmlListener implements View.OnClickListener
{
@Override
public void onClick(View v)
{
String path = txtPath.getText().toString();
try
{
String html = HtmlService.getHtml(path);
txtViewHtml.setText(html);
}
catch (Exception e)
{
Toast.makeText(MainActivity.this, "获取网页元素失败", Toast.LENGTH_SHORT).show();
}
}
}
}

package cn.xy.html.service;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import cn.xy.html.util.IOUtils;
/**
* Html获取业务类
* @author 徐越
*/
public class HtmlService
{
/**
* 获取网页html源代码
* @param path
* @return
*/
public static String getHtml(String path) throws Exception
{
String html = "";
// 把路径包装成URL对象
URL url = new path);
// 基于http协议的连接对象
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// 超时时间5s
conn.setReadTimeout(5000);
// 获取传输方式
conn.setRequestMethod("GET");
// 若响应码为200说明请求成功
if(200 == conn.getResponseCode())
{
InputStream instream = conn.getInputStream();
byte[] data = IOUtils.read(instream);
// 真实情况是读出请求头的charset值
html = new String(data,"UTF-8");
}
return html;
}
}

package cn.xy.html.util;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* IO操作工具类
* @author 徐越
*/
public class IOUtils
{
/**
* 获取输入流的方法
*/
public static byte[] read(InputStream instream) throws IOException
{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = instream.read(buffer)) != -1)
{
bos.write(buffer, 0, len);
}
return bos.toByteArray();
}
}


<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="网络页面路径"
/>
<!-- 网址输入不能使localhost或127.0.0.1 -->
<!-- 因为android是一个操作系统,输入localhost或127.0.0.1会到本操作系统下去找某web应用,所以要使用局域网的ip -->
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/txtPath"
android:text="http://xxx.xxx.xxx.xxx:8080/ad_20_web/test.html"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="获取html"
android:id="@+id/btnShowHtml"
/>
<ScrollView android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/txtViewHtml" />
</ScrollView>

ScrollView标签为TextView增加滚动条。

当然不能忘记访问网络需要权限

<!-- 访问网络权限 -->
<uses-permission android:name="android.permission.INTERNET" />


三、总结

HtmlService中的方法其实可以获取任意类型的数据,因为其中一个环节是获取了byte[],拿到这个字节数组后我们可以根据不同类型的数据进行不同的操作。比如拿到一个图片byte[],就需要使用Bitmap工厂将其转化为Bitmap然后赋给ImageView控件。所以我们要熟悉获取网络资源的一般步骤。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Android