使用Thread+HttpClient+handler

package com.example.slx.slx12_30;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.GridView;

import com.google.gson.Gson;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.List;

//显示一个GridView列表,使用Thread+HttpClient+handler
public class ShowActivity extends AppCompatActivity {

    private GridView gridview;
    private String cid;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Shows shows=(Shows)msg.obj;
            List<Shows.DataBean.ListBean> list = shows.getData().get(0).getList();
            MyAdapter2 myAdapter2 = new MyAdapter2(ShowActivity.this, list);
            gridview.setAdapter(myAdapter2);
        }

    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show);

        gridview = findViewById(R.id.gridview);

        Intent intent = getIntent();
        cid = intent.getStringExtra("cid");

        new Thread(){
            @Override
            public void run() {
                DefaultHttpClient defaultHttpClient = new DefaultHttpClient();
                HttpGet httpGet = new HttpGet("https://www.zhaoapi.cn/product/getProductCatagory?cid="+ cid);
                try {
                    HttpResponse response = defaultHttpClient.execute(httpGet);
                    if(response.getStatusLine().getStatusCode()==200){
                        HttpEntity entity = response.getEntity();
                        String s = EntityUtils.toString(entity);
                        Gson gson = new Gson();
                        Shows shows = gson.fromJson(s, Shows.class);

                        Message obtain = Message.obtain();
                        obtain.obj = shows;
                        handler.sendMessage(obtain);

                    }

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

    }
}

package com.example.slx.slx12_30;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

/**
 * Created by lenovo on 2017/12/30.
 */

public class MyAdapter2 extends BaseAdapter{
    private Context context;
    private List<Shows.DataBean.ListBean> list;

    public MyAdapter2(Context context, List<Shows.DataBean.ListBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if(view==null){
            view=View.inflate(context,R.layout.griditemlayout,null);
            holder=new ViewHolder();
            holder.imageview2=(ImageView)view.findViewById(R.id.imageview2);
            holder.textview2=(TextView)view.findViewById(R.id.textview2);
            view.setTag(holder);
        }else {
            holder=(ViewHolder)view.getTag();

        }
        holder.textview2.setText(list.get(i).getName());

        MyPicTask myPicTask = new MyPicTask(holder.imageview2);
        myPicTask.execute(list.get(i).getIcon());

        return view;

    }

    public class ViewHolder{
        TextView textview2;
        ImageView imageview2;
    }

    public class MyPicTask extends AsyncTask<String,Void,Bitmap> {
        private ImageView img;
        public MyPicTask(ImageView img) {
            this.img = img;
        }
        @Override
        protected Bitmap doInBackground(String... strings) {

            try {
                URL url = new URL(strings[0]);
                HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
                urlConnection.setRequestMethod("GET");
                urlConnection.setReadTimeout(5000);
                urlConnection.setConnectTimeout(5000);
                if(urlConnection.getResponseCode()==200) {
                    InputStream inputStream = urlConnection.getInputStream();
                    Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                    return bitmap;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            if(bitmap==null){
                img.setImageResource(R.mipmap.ic_launcher);
            }else {
                img.setImageBitmap(bitmap);
            }
        }
    }
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值