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); } } } }
使用Thread+HttpClient+handler
最新推荐文章于 2024-09-29 16:20:55 发布