Android获取网络资源

来源:互联网 发布:java基本数据类型字节 编辑:程序博客网 时间:2024/05/22 13:20

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

 

二、实例代码

publicclass MainActivity extends Activity
{
 private EditText txtPath;
 private Button btnShowHtml;
 private TextView txtViewHtml;
 
 @Override
 public void onCreate(BundlesavedInstanceState)
 {
  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(newShowHtmlListener());
 }
 
 private final class ShowHtmlListenerimplements View.OnClickListener
 {
  @Override
  public void onClick(Viewv)
  {
   Stringpath = txtPath.getText().toString();
   try
   {
    Stringhtml = 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;

public class HtmlService
{
 
 public static String getHtml(String path)throws Exception
 {
  String html = "";
  // 把路径包装成URL对象
  URL url = newURL(path);
  // 基于http协议的连接对象
  HttpURLConnection conn =(HttpURLConnection) url.openConnection();
  // 超时时间5s
  conn.setReadTimeout(5000);
  // 获取传输方式
  conn.setRequestMethod("GET");
  // 若响应码为200说明请求成功
  if(200 ==conn.getResponseCode())
  {
   InputStreaminstream = 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;

public class IOUtils
{
 
 public static byte[] read(InputStreaminstream) throws IOException
 {
  ByteArrayOutputStreambos = new ByteArrayOutputStream();
  byte[] buffer = newbyte[1024];
  int len = 0;
  while ((len =instream.read(buffer)) != -1)
  {
   bos.write(buffer,0, len);
  }
  returnbos.toByteArray();
 }
}

 

    
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:text="网络页面路径"
    />
    
    
    
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:id="@+id/txtPath"
     android:text="http://***.***.***.***:8080/ad_20_web/test.html"
    />
    
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="获取html"
     android:id="@+id/btnShowHtml"
    />
    
     
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:id="@+id/txtViewHtml"/>
    

 

ScrollView标签为TextView增加滚动条。

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


 

三、总结
HtmlService中的方法其实可以获取任意类型的数据,因为其中一个环节是获取了byte[],拿到这个字节数组后我们可以根据不同类型的数据进行不同的操作。比如拿到一个图片byte[],就需要使用Bitmap工厂将其转化为Bitmap然后赋给ImageView控件。所以我们要熟悉获取网络资源的一般步骤。

原创粉丝点击