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
  • 相关阅读:
    /bin/bash^M: bad interpreter: No such file or dire
    ****LINUX命令(含GIT命令)个人总结
    创建和编辑 crontab 文件
    Linux下用于查看系统当前登录用户信息的4种方法
    linux下cat命令详解
    crontab 指定执行用户
    crontab定时运行git命令 更新代码库
    ubuntu添加环境变量【原创】
    ubuntu下设置环境变量的三种方法【转】
    笔记三、apache搭建gitweb【转】
  • 原文地址:https://www.cnblogs.com/androidxufeng/p/3653574.html
Copyright © 2011-2022 走看看