请求图片

              请求图片

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;
        }
    }
}


阅读更多
上一篇周考三
想对作者说点什么? 我来说一句

AFNetworking3.0

2016年07月07日 114KB 下载

volley post.get请求图片请求

2016年10月27日 28.54MB 下载

访问网络,请求图片

2015年09月25日 2.44MB 下载

请求的几种情况.txt

2010年04月23日 2KB 下载

volleydemo2

2016年06月21日 852KB 下载

网络图片查看器

2014年11月30日 8KB 下载

没有更多推荐了,返回首页

关闭
关闭