public class MainActivity extends AppCompatActivity {
? private ListView listView;
? private Handler handler=new Handler(){
? @Override
? public void handleMessage(Message msg) {
? //接收数据...设置适配器
? if (msg.what==0){
? DataBean dataBean=(DataBean) msg.obj;
? list=dataBean.getResult().getData();
? //设置适配器
? MyAdapter myAdapter=new MyAdapter(list, MainActivity.this);
? listView.setAdapter(myAdapter);
? }
? }
? };
? private List
? @Override
? protected void onCreate(Bundle savedInstanceState) {
? super.onCreate(savedInstanceState);
? setContentView(R.layout.activity_main);
? listView=(ListView) findViewById(R.id.list_view);
? //条目的点击事件
? listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
? @Override
? public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
? //传值过去...url路径
? if (list !=null){
? Intent intent=new Intent(MainActivity.this,WebActivity.class);
? intent.putExtra("url",list.get(i).getUrl());
? startActivity(intent);
? }
? }
? });
? }
? public void getTop(View view){
? //点击获取网络数据之前,判断一下网络是否可用...不可用,设置
? if (isNetConnected()){
? getNetData();
? }else {
? //对话框
? AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
? builder.setTitle("警告");
? builder.setMessage("网络不可用,是否设置?");
? builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
? @Override
? public void onClick(DialogInterface dialogInterface, int i) {
? //Settings.ACTION_WIRELESS_SETTINGS
? //Settings.ACTION_WIFI_SETTINGS
? Intent intent=new Intent(Settings.ACTION_WIFI_SETTINGS);
? startActivity(intent);
? }
? });
? builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
? @Override
? public void onClick(DialogInterface dialogInterface, int i) {
? }
? });
? builder.show();
? }
? }
?
? private boolean isNetConnected(){
? //1.获取一个连接管理对象
? ConnectivityManager manager=(ConnectivityManager) MainActivity.this.getSystemService(Context.CONNECTIVITY_SERVICE);
? //2.获取网络连接的信息
? NetworkInfo networkInfo=manager.getActiveNetworkInfo();
? if (networkInfo !=null){
? //返回值就代表网络是否可用
? return networkInfo.isAvailable();
? }
? return false;
? }
?
? private void getNetData() {
? new Thread(){
? @Override
? public void run() {
? //头条服务器地址
? String path="http://v.juhe.cn/toutiao/index";
? try {
? URL url=new URL(path);
? //打开连接
? HttpURLConnection connection=(HttpURLConnection) url.openConnection();
? //设置
? connection.setRequestMethod("POST");
? connection.setConnectTimeout(5000);
? connection.setReadTimeout(5000);
? //post方式要提交参数给服务器...以流的方式提交给服务器
? connection.setDoOutput(true);
? //设置请求参数的类型
? connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
? String params="type=top&key=597b4f9dcb50e051fd725a9ec54d6653";
? //参数以流的形式写给服务器...字节数组的形式写出去
? connection.getOutputStream().write(params.getBytes());
? //响应
? int responseCode=connection.getResponseCode();
? if (responseCode==200){
? //获取输入字节流
? InputStream inputStream=connection.getInputStream();
? String json=streamToString(inputStream,"utf-8");
? Log.i("json",json);
? //解析...解析完成之后发送到主线程,设置适配器
? Gson gson=new Gson();
? DataBean dataBean=gson.fromJson(json, DataBean.class);
? //发送出去
? Message message=Message.obtain();
? message.what=0;
? message.obj=dataBean;
? handler.sendMessage(message);
? }
? } catch (Exception e) {
? e.printStackTrace();
? }
? }
? }.start();
? }
? private String streamToString(InputStream inputStream,String charset) {
? try {
? InputStreamReader inputStreamReader=new InputStreamReader(inputStream,charset);
? BufferedReader bufferedReader=new BufferedReader(inputStreamReader);
? String s=null;
? StringBuilder builder=new StringBuilder();
? while ((s=bufferedReader.readLine()) !=null){
? builder.append(s);
? }
? bufferedReader.close();
? return builder.toString();
? } catch (Exception e) {
? e.printStackTrace();
? }
? return? null;
? }
}
//适配器
public class MyAdapter extends BaseAdapter {
? List
? Context context;
? public MyAdapter(List
? this.list=list;
? this.context=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.item_layout,null);
? holder=new ViewHolder();
? holder.img_01=view.findViewById(R.id.img_01);
? holder.img_02=view.findViewById(R.id.img_02);
? holder.img_03=view.findViewById(R.id.img_03);
? holder.text_author=view.findViewById(R.id.text_author);
? holder.text_title=view.findViewById(R.id.text_title);
? holder.text_time=view.findViewById(R.id.text_time);
? view.setTag(holder);
? }else {
? holder=(ViewHolder) view.getTag();
? }
? //赋值
? holder.text_author.setText(list.get(i).getAuthor_name());
? holder.text_title.setText(list.get(i).getTitle());
? holder.text_time.setText(list.get(i).getDate());
? //图片去加载
? new ImageUtil().getImage(list.get(i).getThumbnail_pic_s(),holder.img_01);
? new ImageUtil().getImage(list.get(i).getThumbnail_pic_s02(),holder.img_02);
? new ImageUtil().getImage(list.get(i).getThumbnail_pic_s03(),holder.img_03);
? return view;
? }
? class ViewHolder{
? TextView text_title;
? TextView text_author;
? TextView text_time;
? ImageView img_01;
? ImageView img_02;
? ImageView img_03;
? }
}
//工具类
public class ImageUtil {
? ImageView imageView;
? private Handler handler=new Handler(){
? @Override
? public void handleMessage(Message msg) {
? if (msg.what==0){
? Bitmap bitmap=(Bitmap) msg.obj;
? imageView.setImageBitmap(bitmap);
? }
? }
? };
?
? public void getImage(final String path, ImageView imageView){
? this.imageView=imageView;
? new Thread(){
? @Override
? public void run() {
? try {
? URL url=new URL(path);
? HttpURLConnection connection=(HttpURLConnection) url.openConnection();
? connection.setRequestMethod("GET");
? connection.setReadTimeout(5000);
? connection.setConnectTimeout(5000);
? //响应的状态码
? int responseCode=connection.getResponseCode();
? if (responseCode==200){
? InputStream inputStream=connection.getInputStream();
? //转为bitmap
? Bitmap bitmap=BitmapFactory.decodeStream(inputStream);
? Message message=Message.obtain();
? message.what=0;
? message.obj=bitmap;
? handler.sendMessage(message);
? }
? } catch (Exception e) {
? e.printStackTrace();
? }
? }
? }.start();
? }
}