资讯专栏INFORMATION COLUMN

Android 获取网页上数据的三种方式

娣辩孩 / 1441人阅读

摘要:第一种获取参数后给远程服务器将获得的返回结果返回给调用者本函数适用于查询数量较少的时候第二种获取参数指定的网页代码,将其返回给调用者,由调用者对其解析返回第三种获取指定地址的网页数据返回数据流

第一种

/**获取参数(ArrayList nameValuePairs,String url)后post给远程服务器 
* 将获得的返回结果(String)返回给调用者 
* 本函数适用于查询数量较少的时候 
* Chen.Zhidong 
* 2011-02-15*/ 
public String posturl(ArrayList nameValuePairs,String url){ 
String result = ""; 
String tmp= ""; 
InputStream is = null; 
try{ 
HttpClient httpclient = new DefaultHttpClient(); 
HttpPost httppost = new HttpPost(url); 
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
HttpResponse response = httpclient.execute(httppost); 
HttpEntity entity = response.getEntity(); 
is = entity.getContent(); 
}catch(Exception e){ 
return "Fail to establish http connection!"; 
} 
try{ 
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8")); 
StringBuilder sb = new StringBuilder(); 
String line = null; 

while ((line = reader.readLine()) != null) { 
sb.append(line + "
"); 
} 
is.close(); 
tmp=sb.toString(); 
}catch(Exception e){ 
return "Fail to convert net stream!"; 
} 
try{ 
JSONArray jArray = new JSONArray(tmp); 


for(int i=0;i keys=json_data.keys(); 

while(keys.hasNext()){ 
result += json_data.getString(keys.next().toString()); 
} 
} 
}catch(JSONException e){ 
return "The URL you post is wrong!"; 
} 
return result; 
} 

第二种

/**获取参数指定的网页代码,将其返回给调用者,由调用者对其解析 
* 返回String 
* Chen.Zhidong 
* 2011-02-15*/ 
public String posturl(String url){ 
InputStream is = null; 
String result = ""; 
try{ 
HttpClient httpclient = new DefaultHttpClient(); 
HttpPost httppost = new HttpPost(url); 
HttpResponse response = httpclient.execute(httppost); 
HttpEntity entity = response.getEntity(); 
is = entity.getContent(); 
}catch(Exception e){ 
return "Fail to establish http connection!"+e.toString(); 
} 

try{ 
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8")); 
StringBuilder sb = new StringBuilder(); 
String line = null; 
while ((line = reader.readLine()) != null) { 
sb.append(line + "
"); 
} 

is.close(); 
result=sb.toString(); 
}catch(Exception e){ 
return "Fail to convert net stream!"; 
} 
return result; 
} 

第三种

/**获取指定地址的网页数据 
* 返回数据流 
* Chen.Zhidong 
* 2011-02-18*/ 

public InputStream streampost(String remote_addr){ 
 URL infoUrl = null; 
InputStream inStream = null; 
 try { 
infoUrl = new URL(remote_addr); 
URLConnection connection = infoUrl.openConnection(); 
HttpURLConnection httpConnection = (HttpURLConnection)connection; 
int responseCode = httpConnection.getResponseCode(); 
 if(responseCode == HttpURLConnection.HTTP_OK){ 
 inStream = httpConnection.getInputStream(); 
} 
} catch (MalformedURLException e) { 
 // TODO Auto-generated catch block 
 e.printStackTrace(); 
} catch (IOException e) { 
// TODO Auto-generated catch block 
e.printStackTrace(); 
} 
 return inStream; 
 } 

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/12059.html

相关文章

  • Android组件三种点击事件写法

    摘要:代码如下控件的获取需要传入接口实现类的实例。提示此方法不是一个通用的方法,但对按钮等常用组件的点击事件处理确实很方便。一、准备工作 1、项目结构 2、布局文件(activity_main.xml)   1 2 8 9 14 15   二、效果 三、三种实现方式 1、(方式一)在与事件绑定时定义事件监听器 该方...

    Cheng_Gang 评论0 收藏0
  • SAP Fiori应用三种部署方式

    摘要:目前成都所有基于云的产品开发团队无一例外地采用了这种方式。值得一提的是虽然其后台仍然基于但是的应用的部署方式没有采用本文介绍的方式一,而是本章节正在介绍的方式二。 showImg(https://segmentfault.com/img/remote/1460000013266451); 封面图片来自Google搜索,关键字: Fiori Deployment 方式1 On premi...

    cyrils 评论0 收藏0
  • SAP Fiori应用三种部署方式

    摘要:目前成都所有基于云的产品开发团队无一例外地采用了这种方式。值得一提的是虽然其后台仍然基于但是的应用的部署方式没有采用本文介绍的方式一,而是本章节正在介绍的方式二。 showImg(https://segmentfault.com/img/remote/1460000013266451); 封面图片来自Google搜索,关键字: Fiori Deployment 方式1 On premi...

    _ipo 评论0 收藏0
  • SAP Fiori应用三种部署方式

    摘要:目前成都所有基于云的产品开发团队无一例外地采用了这种方式。值得一提的是虽然其后台仍然基于但是的应用的部署方式没有采用本文介绍的方式一,而是本章节正在介绍的方式二。 showImg(https://segmentfault.com/img/remote/1460000013266451); 封面图片来自Google搜索,关键字: Fiori Deployment 方式1 On premi...

    Zachary 评论0 收藏0
  • SAP Fiori应用三种部署方式

    摘要:目前成都所有基于云的产品开发团队无一例外地采用了这种方式。值得一提的是虽然其后台仍然基于但是的应用的部署方式没有采用本文介绍的方式一,而是本章节正在介绍的方式二。 showImg(https://segmentfault.com/img/remote/1460000013266451); 封面图片来自Google搜索,关键字: Fiori Deployment 方式1 On premi...

    henry14 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<