Android获取网页数据的方法总结

发布时间:2011年07月18日      浏览次数:643 次
本文总结了三种获取网页数据的代码,是自己在用的时候随手整理出来的。此处仅贴出函数段,不贴出import了,用的时候可以用eclipse自动import一下就行了。函数的详细用途描述请看代码中注释。调用的时候请对应函数需要的参数。

001 //第一种

002 /**获取参数(ArrayList<NameValuePair> nameValuePairs,String url)后post给远程服务器

003 * 将获得的返回结果(String)返回给调用者

004 * 本函数适用于查询数量较少的时候

005 * Chen.Zhidong

006 * 2011-02-15*/

007 public String posturl(ArrayList<NameValuePair> nameValuePairs,String url){

008 String result = "";

009 String tmp= "";

010 InputStream is = null;

011 try{

012 HttpClient httpclient = new DefaultHttpClient();

013 HttpPost httppost = new HttpPost(url);

014 httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

015 HttpResponse response = httpclient.execute(httppost);

016 HttpEntity entity = response.getEntity();

017 is = entity.getContent();

018 }catch(Exception e){

019 return "Fail to establish http connection!";

020 }

021 try{

022 BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"));

023 StringBuilder sb = new StringBuilder();

024 String line = null;

025 while ((line = reader.readLine()) != null) {

026 sb.append(line + "\n");

027 }

028 is.close();

029 tmp=sb.toString();

030 }catch(Exception e){

031 return "Fail to convert net stream!";

032 }

033 try{

034 JSONArray jArray = new JSONArray(tmp);

035 for(int i=0;i<jArray.length();i++){

036 JSONObject json_data = jArray.getJSONObject(i);

037 Iterator<?> keys=json_data.keys();

038 while(keys.hasNext()){

039 result += json_data.getString(keys.next().toString());

040 }

041 }

042 }catch(JSONException e){

043 return "The URL you post is wrong!";

044 }

045 return result;

046 }

047 //第二种

048 /**获取参数指定的网页代码,将其返回给调用者,由调用者对其解析

049 * 返回String

050 * Chen.Zhidong

051 * 2011-02-15*/

052 public String posturl(String url){

053 InputStream is = null;

054 String result = "";

055 try{

056 HttpClient httpclient = new DefaultHttpClient();

057 HttpPost httppost = new HttpPost(url);

058 HttpResponse response = httpclient.execute(httppost);

059 HttpEntity entity = response.getEntity();

060 is = entity.getContent();

061 }catch(Exception e){

062 return "Fail to establish http connection!"+e.toString();

063 }

064 try{

065 BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"));

066 StringBuilder sb = new StringBuilder();

067 String line = null;

068 while ((line = reader.readLine()) != null) {

069 sb.append(line + "\n");

070 }

071 is.close();

072 result=sb.toString();

073 }catch(Exception e){

074 return "Fail to convert net stream!";

075 }

076 return result;

077 }

078 //第三种

079 /**获取指定地址的网页数据

080 * 返回数据流

081 * Chen.Zhidong

082 * 2011-02-18*/

083 public InputStream streampost(String remote_addr){

084 URL infoUrl = null;

085 InputStream inStream = null;

086 try {

087 infoUrl = new URL(remote_addr);

088 URLConnection connection = infoUrl.openConnection();

089 HttpURLConnection httpConnection = (HttpURLConnection)connection;

090 int responseCode = httpConnection.getResponseCode();

091 if(responseCode == HttpURLConnection.HTTP_OK){

092 inStream = httpConnection.getInputStream();

093 }

094 } catch (MalformedURLException e) {

095 // TODO Auto-generated catch block

096 e.printStackTrace();

097 } catch (IOException e) {

098 // TODO Auto-generated catch block

099 e.printStackTrace();

100 }

101 return inStream;

102 }
免责声明:本站相关技术文章信息部分来自网络,目的主要是传播更多信息,如果您认为本站的某些信息侵犯了您的版权,请与我们联系,我们会即时妥善的处理,谢谢合作!