请求图片

原创 2018年04月16日 07:57:54

              请求图片

package com.example.day01_demo_a;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class MainActivity extends AppCompatActivity {

    //ctrl+alt+f 变为成员变量
    private Button btnDown;
    private ImageView img;

    private Handler mHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            //super.handleMessage(msg);
            Bitmap bit = (Bitmap) msg.obj;
            img.setImageBitmap(bit);


        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //alt+enter 导包
        btnDown = findViewById(R.id.btn_down);
        img = findViewById(R.id.img);


        btnDown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //访问网络,获取图片
                new Thread(){
                    @Override
                    public void run() {
                        requestNetData();
                    }
                }.start();
            }
        });






    }

    private void requestNetData() {
        //ctrl+alt+t  生成try...catch...块
        try {
            //1.创建URL
            URL url=new URL("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1523341929854&di=06c172116bfe5f05237691c575536d80&imgtype=0&src=http%3A%2F%2Fpic112.nipic.com%2Ffile%2F20160918%2F20897729_085852947000_2.jpg");
            //2.打开连接  HttpURLConnection 是 URLConnection 的子类
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            //3.进行设置
            urlConnection.setRequestMethod("GET");//请求方式 GET要大写
            urlConnection.setReadTimeout(5000);//设置读取超时时间
            urlConnection.setConnectTimeout(5000);//设置连接超时时间

            //4.判断响应码
            int responseCode = urlConnection.getResponseCode();//得到响应码
            if(responseCode==200){//200代表成功
                //5.获取服务器返回的数据  ----以流的形式返回的
                InputStream inputStream = urlConnection.getInputStream();

                //将流转换成一个bitmap对象
                Bitmap bitmap = BitmapFactory.decodeStream(inputStream);

                //显示
                //img.setImageBitmap(bitmap);
                Message msg=Message.obtain();
                msg.obj=bitmap;
                mHandler.sendMessage(msg);

                //ctrl+y 删除一行

            }

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

    }
}




package com.example.day01_demo_a;

import java.util.List;

/**
 * Created by jane on 2018/4/10.
 */

public class ResultData {
    /**
     * code : 200
     * msg : success
     * newslist : [{"ctime":"2018-04-10 00:00","title":"40岁大姐假扮90后骗走情郎600万 警方公布嫌疑人照片","description":"凤凰社会","picUrl":"http://d.ifengimg.com/w150_h95/p0.ifengimg.com/fck/2018_15/62a0e528a1be0e1_w640_h1097.jpg","url":"http://news.ifeng.com/a/20180410/57447229_0.shtml"},{"ctime":"2018-04-10 00:00","title":"老教授突然性情大变爱上恶作剧 原来是患了这种病","description":"凤凰社会","picUrl":"http://d.ifengimg.com/w150_h95/p0.ifengimg.com/pmop/2018/0410/851A4AD80F48DD558C04F63FAECD81959C9BE28B_size90_w300_h399.jpeg","url":"http://news.ifeng.com/a/20180410/57441195_0.shtml"},{"ctime":"2018-04-10 00:00","title":"杜特尔特来博鳌,要帮10万菲律宾教师找工作","description":"凤凰社会","picUrl":"http://d.ifengimg.com/w150_h95/p1.ifengimg.com/fck/2018_15/b24cf0188187e3a_w400_h320.jpg","url":"http://news.ifeng.com/a/20180410/57441287_0.shtml"},{"ctime":"2018-04-10 00:00","title":"广东:酒驾、闯红灯将影响贷款和买房","description":"凤凰社会","picUrl":"http://d.ifengimg.com/w150_h95/p1.ifengimg.com/fck/2018_15/b24cf0188187e3a_w400_h320.jpg","url":"http://news.ifeng.com/a/20180410/57440944_0.shtml"},{"ctime":"2018-04-10 00:00","title":"乱!\u201c黑外教\u201d学历全造假 家长只认外国脸","description":"凤凰社会","picUrl":"http://d.ifengimg.com/w150_h95/p1.ifengimg.com/fck/2018_15/b24cf0188187e3a_w400_h320.jpg","url":"http://news.ifeng.com/a/20180410/57439535_0.shtml"},{"ctime":"2018-04-09 00:00","title":"去世小夫妻遗留受精胚胎 4老人寻求代孕最终产子","description":"凤凰社会","picUrl":"http://d.ifengimg.com/w150_h95/p3.ifengimg.com/a/2018_15/0323ae989c33ed2_size32_w640_h422.jpg","url":"http://news.ifeng.com/a/20180409/57438737_0.shtml"},{"ctime":"2018-04-10 00:00","title":"钱宝集资人非法维权内幕:血本无归后听信谣言","description":"凤凰社会","picUrl":"http://d.ifengimg.com/w150_h95/p0.ifengimg.com/pmop/2018/0409/9161EF8F01C9703FEA9FE7285C6143428A27BE89_size73_w1080_h810.jpeg","url":"http://news.ifeng.com/a/20180410/57438901_0.shtml"},{"ctime":"2018-04-09 00:00","title":"妈妈女友同落水先救谁?高校女教师:先救女友违法","description":"凤凰社会","picUrl":"http://d.ifengimg.com/w150_h95/p3.ifengimg.com/a/2018_15/0323ae989c33ed2_size32_w640_h422.jpg","url":"http://news.ifeng.com/a/20180409/57438185_0.shtml"},{"ctime":"2018-04-09 00:00","title":"九旬老人无法取老伴30万遗款 银行:先办继承公证","description":"凤凰社会","picUrl":"http://d.ifengimg.com/w150_h95/p3.ifengimg.com/a/2018_15/58bb75ea151e3c0_size41_w400_h302.jpg","url":"http://news.ifeng.com/a/20180409/57437996_0.shtml"},{"ctime":"2018-04-09 00:00","title":"扇巴掌、踢肚子、鞋刷抽孩子 又一家\u201c豫章书院\u201d出现","description":"凤凰社会","picUrl":"http://d.ifengimg.com/w150_h95/p3.ifengimg.com/fck/2018_15/54ac747712a3601_w353_h269.jpg","url":"http://news.ifeng.com/a/20180409/57438051_0.shtml"}]
     */

    private int code;
    private String msg;
    private List<NewslistBean> newslist;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public List<NewslistBean> getNewslist() {
        return newslist;
    }

    public void setNewslist(List<NewslistBean> newslist) {
        this.newslist = newslist;
    }

    public static class NewslistBean {
        /**
         * ctime : 2018-04-10 00:00
         * title : 40岁大姐假扮90后骗走情郎600万 警方公布嫌疑人照片
         * description : 凤凰社会
         * picUrl : http://d.ifengimg.com/w150_h95/p0.ifengimg.com/fck/2018_15/62a0e528a1be0e1_w640_h1097.jpg
         * url : http://news.ifeng.com/a/20180410/57447229_0.shtml
         */

        private String ctime;
        private String title;
        private String description;
        private String picUrl;
        private String url;

        public String getCtime() {
            return ctime;
        }

        public void setCtime(String ctime) {
            this.ctime = ctime;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;
        }

        public String getPicUrl() {
            return picUrl;
        }

        public void setPicUrl(String picUrl) {
            this.picUrl = picUrl;
        }

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }
    }
}


android get请求服务器图片

 每次总结都只能一小段一小段的进行总结,最开始一行一行的看代码,后来是一段一段得看,再后来一项一项的看,目前只有这个水平,不过坚持不懈后面会一个项目一个项目的看,转正题: 请求服务器上的图片要...
  • generallizhong
  • generallizhong
  • 2015年10月27日 10:34
  • 837

HttpURLConnection请求图片

效果图:页面代码:public class HttpJpgActivity extends Activity { private ImageView view; private Han...
  • pengkv
  • pengkv
  • 2015年08月13日 17:12
  • 1432

python 处理请求获取的图片

环境:python2 (目前使用的环境,虽然比较喜欢使用python3) 图片只有一个图片链接 http://img2.suv.cn/2018/0115/1515977939198935.jpg ...
  • a1368783069
  • a1368783069
  • 2018年01月18日 11:20
  • 156

请求图片 get方式

package zhanghaijiao.bawei.com.test1511q; // import android.graphics.Bitmap; import android.gra...
  • wwe11122
  • wwe11122
  • 2018年01月11日 20:02
  • 35

图片的HTTP请求总结,web图片http请求汇总,网页图片预加载

图片的http请求,有很多种情况,那么究竟什么情况下面不会发生请求呢?下面我用案例一一列举一下,希望对你深入了解http图片请求有所帮助。(请求图片截图,均已谷歌浏览器为例!文章转载请联系我,或者注明...
  • jspshuishui
  • jspshuishui
  • 2015年11月16日 17:01
  • 1700

css sprite技术 – 减少网站图片请求次数

CSS Sprites在国内很多人叫css精灵,是一种网页图片应用处理方式。它允许你将一个页面涉及到的所有零星图片都包含到一张大图中去,这样一来,当访问该页面时,载入的图片就不会像以前那样一幅一幅地慢...
  • u011144211
  • u011144211
  • 2015年07月07日 11:15
  • 880

http请求返回图片及图片调整大小

///         /// 请求htpp 返回图片         ///         ///         public static Bitmap HttpPostOutI...
  • windowsliusheng
  • windowsliusheng
  • 2015年11月20日 17:51
  • 550

图片合并 减少 http 请求

代码如下:  复制代码 代码如下:                 注意:这五个按钮分别使用了五张图片  那么,五张图片就意味着你的该页面又多了五...
  • JackieLiuLixi
  • JackieLiuLixi
  • 2014年08月14日 11:20
  • 943

请求图片

package com.wzq.listviewimage; import android.graphics.Bitmap; import android.os.AsyncTask; import ...
  • qq_40087961
  • qq_40087961
  • 2017年09月03日 21:06
  • 88

图片请求

AS name=new AS();         name.execute();     }     class AS extends AsyncTask {         private...
  • KingYijie
  • KingYijie
  • 2016年04月06日 08:35
  • 42
收藏助手
不良信息举报
您举报文章:请求图片
举报原因:
原因补充:

(最多只允许输入30个字)