Android获取网页信息的方法

发布时间:2011年07月21日      浏览次数:1014 次
Test_Get_WEB_TXT.java 代码如下:
---------------------------------------------------------------------------------------------
方法一:(本方法可以指定获取内容的编码,解决获取中文时出现乱码的问题)
---------------------------------------------------------------------------------------------
package com.android.MySoft;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class Test_Get_WEB_TXT extends Activity {      
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_get_web_txt);
            
setTitle("测试获取网络文本");

String url="http://www.***.com/test.html";

TextView tv = new TextView(this);
String GET_DATA = "";
try {
      GET_DATA = getHtml(url,"UTF-8");
            } catch (Exception e) {
                  GET_DATA = "获取数据错误:" + e.toString();
                  //e.printStackTrace();
            }
tv.setText(GET_DATA);
setContentView(tv);             
}

private String getHtml(String urlpath, String encoding) throws Exception {
            URL url = new URL(urlpath);
            // 实例化一个HTTP连接对象conn
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            // 定义请求方式为GET,其中GET的格式需要注意
            conn.setRequestMethod("GET");
            // 定义请求时间,在ANDROID中最好是不好超过10秒。否则将被系统回收。
            conn.setConnectTimeout(6 * 1000);
            if (conn.getResponseCode() == 200) {
                  InputStream inStream = conn.getInputStream();
                  byte[] data = readStream(inStream);
                  return new String(data, encoding);
            }
            return "";
      }

      private byte[] readStream(InputStream inStream) throws Exception {
            ByteArrayOutputStream outStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len = -1;
            // 将输入流不断的读,并放到缓冲区中去。直到读完
            while ((len = inStream.read(buffer)) != -1) {
                  // 将缓冲区的数据不断的写到内存中去。
                  outStream.write(buffer, 0, len);
            }
            outStream.close();
            inStream.close();
            return outStream.toByteArray();
      }
}

---------------------------------------------------------------------------------------------
方法二:
---------------------------------------------------------------------------------------------
package com.android.MySoft;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class Test_Get_WEB_TXT extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_get_web_txt);
            
setTitle("测试获取网络文本");

String url="http://www.***.com/test.html";

TextView tv = new TextView(this);
String GET_DATA = "";
GET_DATA =
tv.setText(GET_DATA);
setContentView(tv);             
}
/**
* 请求服务
* @param url
*/
private String Get_Data(String url){
      String Result = "";
HttpClient client=new DefaultHttpClient();
HttpPost request;
try {
       request = new HttpPost(new URI(url));
       HttpResponse response = client.execute(request);

       //判断请求是否成功       
       if(response.getStatusLine().getStatusCode()==200){
             HttpEntity entity=response.getEntity();
             if(entity!=null){
                   Result = EntityUtils.toString(entity); //返回获取的值
             } else{
                   Result = "没有获取到任何数据!";      //返回空       
             }
       }
                  
}catch (URISyntaxException e) {
      Result = "错误:网址!";      
      //e.printStackTrace();
}catch (ClientProtocolException e) {
      Result = "错误:客户端协议!";      
      //e.printStackTrace();
}catch (IOException e) {
      Result = "错误:输入输出!";       
      //e.printStackTrace();
}
      return Result;
}
}


---------------------------------------------------------------------------------------------
test_get_web_txt.xml 代码如下:
---------------------------------------------------------------------------------------------

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
免责声明:本站相关技术文章信息部分来自网络,目的主要是传播更多信息,如果您认为本站的某些信息侵犯了您的版权,请与我们联系,我们会即时妥善的处理,谢谢合作!