Android手机开发:网络连接-打开Url下载信息

Linux大全评论769 views阅读模式

1. 简单版本

根据URL下载文件,前提是这个文件当中的内容是文本,函数的返回值就是文件当中的内容

1.创建一个URL对象

2.通过URL对象,创建一个HttpURLConnection对象 3.得到InputStram 4.从InputStream当中读取数据

  1. public String getTextFromUrl(String urlStr) {  
  2.     StringBuffer sb = new StringBuffer();  
  3.     String line = null;  
  4.     BufferedReader buffer = null;  
  5.     try {  
  6.         // 创建一个URL对象   
  7.         url = new URL(urlStr);  
  8.         // 创建一个Http连接   
  9.         HttpURLConnection urlConn = (HttpURLConnection) url  
  10.                 .openConnection();  
  11.         // 使用IO流读取数据   
  12.         buffer = new BufferedReader(new InputStreamReader(  
  13.                 urlConn.getInputStream()));  
  14.         while ((line = buffer.readLine()) != null) {  
  15.             sb.append(line);  
  16.         }  
  17.     } catch (Exception e) {  
  18.         e.printStackTrace();  
  19.     } finally {  
  20.         try {  
  21.             buffer.close();  
  22.         } catch (Exception e) {  
  23.             e.printStackTrace();  
  24.         }  
  25.     }  
  26.     return sb.toString();  
  27. }  
  1. //发送http信息,并信息进行编码   
  2. public String SendDataPost(String url, String post)  
  3.    {  
  4.     // 判断网络链接是否正常   
  5.     if (isNetworkAvailable(fromcon)) {  
  6.         try  
  7.         {  
  8.             String  mString = new String(post.getBytes(), "UTF-8");  
  9.             URL iurl = new URL(url);//直接提交地址,不要带参数           
  10.             HttpURLConnection objConn = (HttpURLConnection)iurl.openConnection();  
  11.             //objConn.setRequestProperty("Cookie",HttpTools.PublishCookies());   
  12.             
  13.             objConn.setDoOutput(true);  
  14.             objConn.setDoInput(true);  
  15.             objConn.setRequestProperty("Content-type","application/x-www-form-urlencoded");  
  16.             objConn.setRequestMethod("POST");  
  17.             objConn.setRequestProperty("Content-Length",String.valueOf(mString.toCharArray().length));  
  18.             objConn.setConnectTimeout(30000);  
  19.             objConn.setReadTimeout(30000);  
  20.             objConn.connect();  
  21.               
  22.             OutputStream  objSM = objConn.getOutputStream();  
  23.             OutputStreamWriter objSW = new OutputStreamWriter(objSM);  
  24.             BufferedWriter  out = new BufferedWriter(objSW);  
  25.                         
  26.             out.write(mString.toCharArray(),0,mString.toCharArray().length);  
  27.             out.flush();  
  28.             out.close();  
  29.              
  30.             InputStream objSMP = objConn.getInputStream();  
  31.             InputStreamReader objSRP = new InputStreamReader(objSMP, "utf-8");  
  32.             BufferedReader in = new BufferedReader(objSRP);    
  33.             String line = null;    
  34.             StringBuilder sb = new StringBuilder();    
  35.             while ((line = in.readLine()) != null) {    
  36.               sb.append(line);    
  37.             }    
  38.             in.close();   
  39.             String resp = sb.toString();  
  40.             objSMP.close();  
  41.             objConn.disconnect();  
  42.             return resp;  
  43.         }catch (Exception ex) {  
  44.             Log.i("CCCC", ex.toString());  
  45.             return "";  
  46.         }  
  47.     }else{  
  48.       
  49.         Intent intent0 = new Intent(fromcon,  
  50.             LoginActivity.class);  
  51.         intent0.putExtra("msg",  
  52.             "您当前网络连接已禁用,请重新设置!");  
  53.         fromcon.startActivity(intent0);  
  54.         return "";  
  55.     }  
  56.    }  
  57.   
  58. // 判断网络是否正常   
  59. public static boolean isNetworkAvailable(Context context) {  
  60.     ConnectivityManager connectivity = (ConnectivityManager) context  
  61.             .getSystemService(Context.CONNECTIVITY_SERVICE);  
  62.     if (connectivity == null) {  
  63.           
  64.         return false;  
  65.     } else {  
  66.         NetworkInfo info = connectivity.getActiveNetworkInfo();  
  67.         if (info == null) {  
  68.               
  69.             return false;  
  70.         } else {  
  71.             if (info.isAvailable()) {                     
  72.                 return true;  
  73.             }  
  74.         }  
  75.     }  
  76.       
  77.     return false;  
  78. }  

企鹅博客
  • 本文由 发表于 2019年9月4日 20:34:08
  • 转载请务必保留本文链接:https://www.qieseo.com/171056.html

发表评论