HttpUrlConnection

webview

BufferedReader reader=new BufferedReader(new InputStream(coon.getInputStream));
reader.redaLine();
//网站解析出来获取"类型"
webView.loadData(sb.toString,"类型",null);

imageView

URL mUrl = null;
        try {
            mUrl = new URL("网址");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        HttpURLConnection coon = null;
        try {
            coon = (HttpURLConnection) mUrl.openConnection();
            coon.setReadTimeout(5000);
            coon.setRequestMethod("GET");
            coon.setDoInput(true);
            //流
            InputStream in = coon.getInputStream();
            FileOutputStream out = null;
            File file=null;
            String fileName=String.valueOf(System.currentTimeMillis());
            if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
                File parent=Environment.getExternalStorageDirectory();
                file = new File(parent,fileName);
                out = new FileOutputStream(file);

            }
            byte[] b=new byte[1024*2];
            int len;
            if (out != null){
                while ((len=in.read())>0){
                    out.write(b,0,len);
                }
            }

            Bitmap bp= BitmapFactory.decodeFile(file.getAbsolutePath());
            handle.post(new Runnable() {
                @Override
                public void run() {
                    //TODO 主线程UI
                }
            });

        } catch (IOException e) {
            e.printStackTrace();
        }

第二种

//获取到bigmap中
URL httpUrl=new URL("**");
HttpURLConnection coon=(HttpURLConnection)httpUrl.openConnection();
coon.setReadTimeout(5000);
coon.setRequestMethod("GET");
InputStream in=coon.getInputStream();
Bitmap bp=BitmapFactory.decodeStream(in);

POST

较安全 容量大

URL mUrl = null;
        try {
            mUrl = new URL("网址");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        HttpURLConnection coon = null;
        try {
            coon = (HttpURLConnection) mUrl.openConnection();
            coon.setReadTimeout(5000);
            coon.setRequestMethod("POST");
            OutputStream out = coon.getOutputStream();\
           //根据需求
           String content = "sjx"+sjx;
           //写
           out.write(content.getBytes());
           //读
           BufferedReader reader=new BufferedReader(new InputStream(coon.getInputStream));
           StringBuffer sb=new StringBuffer();
           String str;
           while((str=reader.readLine()!=null){
               sb.append(str);
           }

你可能感兴趣的:(android,基础,url)