zoukankan      html  css  js  c++  java
  • 转载 Android获取网页数据的方法总结

    本文总结了三种获取网页数据的代码,是自己在用的时候随手整理出来的。此处仅贴出函数段,不贴出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
    022 try{
    023 BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"));
    024 StringBuilder sb = new StringBuilder();
    025 String line = null;
    026 while ((line = reader.readLine()) != null) {
    027 sb.append(line + "\n");
    028 }
    029 is.close();
    030
    031 tmp=sb.toString();
    032 }catch(Exception e){
    033 return "Fail to convert net stream!";
    034 }
    035
    036 try{
    037 JSONArray jArray = new JSONArray(tmp);
    038 for(int i=0;i<jArray.length();i++){
    039 JSONObject json_data = jArray.getJSONObject(i);
    040 Iterator<?> keys=json_data.keys();
    041 while(keys.hasNext()){
    042 result += json_data.getString(keys.next().toString());
    043 }
    044 }
    045 }catch(JSONException e){
    046 return "The URL you post is wrong!";
    047 }
    048
    049 return result;
    050 }
    051
    052 //第二种
    053 /**获取参数指定的网页代码,将其返回给调用者,由调用者对其解析
    054 * 返回String
    055 * Chen.Zhidong
    056 * 2011-02-15
    */
    057 public String posturl(String url){
    058 InputStream is = null;
    059 String result = "";
    060
    061 try{
    062 HttpClient httpclient = new DefaultHttpClient();
    063 HttpPost httppost = new HttpPost(url);
    064 HttpResponse response = httpclient.execute(httppost);
    065 HttpEntity entity = response.getEntity();
    066 is = entity.getContent();
    067 }catch(Exception e){
    068 return "Fail to establish http connection!"+e.toString();
    069 }
    070
    071 try{
    072 BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"));
    073 StringBuilder sb = new StringBuilder();
    074 String line = null;
    075 while ((line = reader.readLine()) != null) {
    076 sb.append(line + "\n");
    077 }
    078 is.close();
    079
    080 result=sb.toString();
    081 }catch(Exception e){
    082 return "Fail to convert net stream!";
    083 }
    084
    085 return result;
    086 }
    087
    088 //第三种
    089 /**获取指定地址的网页数据
    090 * 返回数据流
    091 * Chen.Zhidong
    092 * 2011-02-18
    */
    093 public InputStream streampost(String remote_addr){
    094 URL infoUrl = null;
    095 InputStream inStream = null;
    096 try {
    097 infoUrl = new URL(remote_addr);
    098 URLConnection connection = infoUrl.openConnection();
    099 HttpURLConnection httpConnection = (HttpURLConnection)connection;
    100 int responseCode = httpConnection.getResponseCode();
    101 if(responseCode == HttpURLConnection.HTTP_OK){
    102 inStream = httpConnection.getInputStream();
    103 }
    104 } catch (MalformedURLException e) {
    105 // TODO Auto-generated catch block
    106 e.printStackTrace();
    107 } catch (IOException e) {
    108 // TODO Auto-generated catch block
    109 e.printStackTrace();
    110 }
    111 return inStream;
    112 }
  • 相关阅读:
    PHP 8.0 带来的新特性
    do sth 之 提取了一份文档
    Java入门15---网络编程
    Java入门14---logback
    限流策略
    JConsole 可视化工具
    SpringBoot注解---6.声明式事务
    SpringBoot注解---5.AOP
    SpringBoot注解---4.扩展原理
    SpringBoot注解---2.组件赋值
  • 原文地址:https://www.cnblogs.com/xiao0/p/2172256.html
Copyright © 2011-2022 走看看