zoukankan      html  css  js  c++  java
  • http的应用httpurlconnection--------1

    http请求后获得所需要的是字符串的时候

                    URL url=new URL(strurl);
                    try {
                        HttpURLConnection conn=(HttpURLConnection) url.openConnection();
                        conn.setConnectTimeout(10*1000);
                        conn.setRequestMethod("GET");
                        conn.setDoInput(true);
                        conn.connect();
                        int size=conn.getContentLength();
                        InputStream input=conn.getInputStream();
                        InputStreamReader inputreader=new InputStreamReader(input);
                        BufferedReader bufferreader=new BufferedReader(inputreader);
                        StringBuffer strbuffer=new StringBuffer();
                        byte [] b=new byte [1024];
                        String  temp;
                        while((temp=bufferreader.readLine())!=null){
                            strbuffer.append(b);
                        }
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                

    文件下载

     /**
    
         * 文件下载
    
         */
    
        private void downloadFile()
    
        {
    
            try {
    
                URL u = new URL(url);
    
                URLConnection conn = u.openConnection();
    
                conn.connect();
    
                InputStream is = conn.getInputStream();
    
                fileSize = conn.getContentLength();
    
                if(fileSize<1||is==null)
    
                {
    
                    sendMessage(DOWNLOAD_ERROR);
    
                }else{
    
                    sendMessage(DOWNLOAD_PREPARE);
    
                    FileOutputStream fos = new FileOutputStream(getPath());
    
                    byte[] bytes = new byte[1024];
    
                    int len = -1;
    
                    while((len = is.read(bytes))!=-1)
    
                    {
    
                        fos.write(bytes, 0, len);
    
                        downloadSize+=len;
    
                        sendMessage(DOWNLOAD_WORK);
    
                    }
    
                    sendMessage(DOWNLOAD_OK);
    
                    is.close();
    
                    fos.close();
    
                }
    
            } catch (Exception e) {
    
                sendMessage(DOWNLOAD_ERROR);
    
                e.printStackTrace();
    
            }
    
        }
    
    
    
        /**
    
         * 得到文件的保存路径
    
         * @return
    
         * @throws IOException
    
         */
    
        private String getPath() throws IOException
    
        {
    
            String path = FileUtil.setMkdir(this)+File.separator+url.substring(url.lastIndexOf("/")+1);
    
            return path;
    
        }

    BitMap对象

    URL  url = new URL(params);
    
    HttpURLConnection conn  = (HttpURLConnection)url.openConnection(); 
    conn.setDoInput(true);
    conn.connect();
    InputStream inputStream=conn.getInputStream();
    bitmap = BitmapFactory.decodeStream(inputStream
  • 相关阅读:
    python之连接oracle数据库
    从一副牌中随机抽一张牌
    判断一个点是否在圆内
    判断每个月有多少天
    猜数字游戏
    求一元二次方程的2个跟
    Servlet细节处理
    Servlet
    Http协议
    Struts2(2)
  • 原文地址:https://www.cnblogs.com/androidxufeng/p/3653574.html
Copyright © 2011-2022 走看看