package fragment.bwie.com.rikao15; import android.os.AsyncTask; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.widget.ListView; import com.google.gson.Gson; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.util.List; import fragment.bwie.com.rikao15.adapter.MyAdapter; import fragment.bwie.com.rikao15.bean.Bean; public class MainActivity extends AppCompatActivity { private ListView listView; private Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); if (msg.what == 0){ Bean bean = (Bean) msg.obj; List<Bean.NewslistBean> list = bean.getNewslist(); MyAdapter myAdapter = new MyAdapter(list, MainActivity.this); listView.setAdapter(myAdapter); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ListView) findViewById(R.id.list_view); AsyncTask<Void, Void, String> asyncTask = new AsyncTask<Void, Void, String>() { @Override protected String doInBackground(Void... voids) { String path = "https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10"; try { URL url = new URL(path); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod("GET"); urlConnection.setReadTimeout(5000); urlConnection.setConnectTimeout(5000); int responseCode = urlConnection.getResponseCode(); if (responseCode == 200) { InputStream inputStream = urlConnection.getInputStream(); String json = streamTostring(inputStream, "utf-8"); return json; } } catch (Exception e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(String s) { super.onPostExecute(s); Gson gson = new Gson(); Bean bean = gson.fromJson(s, Bean.class); Message message = Message.obtain(); message.what=0; message.obj=bean; handler.sendMessage(message); } }; asyncTask.execute(); } private String streamTostring(InputStream inputStream, String s) { try { InputStreamReader inputStreamReader = new InputStreamReader(inputStream, s); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); StringBuilder builder = new StringBuilder(); String string = null; while ((string = bufferedReader.readLine()) != null) { builder.append(string); } bufferedReader.close(); return builder.toString(); } catch (Exception e) { e.printStackTrace(); } return null; } }
--------------------------------------------设配器
package fragment.bwie.com.rikao15.adapter; import android.content.Context; import android.graphics.Bitmap; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; import com.nostra13.universalimageloader.core.DisplayImageOptions; import com.nostra13.universalimageloader.core.ImageLoader; import com.nostra13.universalimageloader.core.ImageLoaderConfiguration; import com.nostra13.universalimageloader.core.assist.ImageScaleType; import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer; import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer; import java.util.List; import fragment.bwie.com.rikao15.R; import fragment.bwie.com.rikao15.bean.Bean; /** * Created by CZ on 2017/9/15. */ public class MyAdapter extends BaseAdapter { List<Bean.NewslistBean> list; Context context; public MyAdapter(List<Bean.NewslistBean> list, Context context) { this.list = list; this.context = context; ImageLoader.getInstance().init(ImageLoaderConfiguration.createDefault(context)); } @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.list_item, null); holder = new ViewHolder(); holder.imageView = (ImageView) view.findViewById(R.id.image); holder.title = (TextView) view.findViewById(R.id.title); holder.xinxi = (TextView) view.findViewById(R.id.xinxi); view.setTag(holder); } else { holder = (ViewHolder) view.getTag(); } holder.title.setText(list.get(i).getDescription()); holder.xinxi.setText(list.get(i).getTitle()); ImageLoader.getInstance().displayImage(list.get(i).getPicUrl(), holder.imageView,getOption()); return view; } private DisplayImageOptions getOption() { DisplayImageOptions options = new DisplayImageOptions.Builder() .showImageOnLoading(R.mipmap.ic_launcher) //设置图片在下载期间显示的图片 .showImageForEmptyUri(R.mipmap.ic_launcher)//设置图的时片Uri为空或是错误候显示的图片 .showImageOnFail(R.mipmap.ic_launcher) //设置图片加载/解码过程中错误时候显示的图片 .cacheInMemory(true)//设置下载的图片是否缓存在内存中 .cacheOnDisk(true)//设置下载的图片是否缓存在SD卡中 .considerExifParams(true) //是否考虑JPEG图像EXIF参数(旋转,翻转) .imageScaleType(ImageScaleType.IN_SAMPLE_INT)//设置图片以如何的编码方式显示 .bitmapConfig(Bitmap.Config.RGB_565)//设置图片的解码类型 //.decodingOptions(BitmapFactory.Options decodingOptions)//设置图片的解码配置 .delayBeforeLoading(0)//int delayInMillis为你设置的下载前的延迟时间 //设置图片加入缓存前,对bitmap进行设置 //.preProcessor(BitmapProcessor preProcessor) .resetViewBeforeLoading(true)//设置图片在下载前是否重置,复位 .displayer(new RoundedBitmapDisplayer(20))//不推荐用!!!!是否设置为圆角,弧度为多少 .displayer(new FadeInBitmapDisplayer(100))//是否图片加载好后渐入的动画时间,可能会出现闪动 .build();//构建完成 return options; } public class ViewHolder { ImageView imageView; TextView title; TextView xinxi; } }
-----------配置
nostra