LunBo+Xlist(Man)

package com.bwei.fragment;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bawei.xlistviewlib.view.XListView;
import com.bwei.adapter.MyListAdapter;
import com.bwei.adapter.MyPagerAdapter;
import com.bwei.bean.DataInfo;
import com.bwei.day14_lianxi7.MainActivity;
import com.bwei.day14_lianxi7.R;
import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.ImageLoader;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 杨柳 on 2018/7/20.
 */

public class Fragment1 extends Fragment {

    private XListView x_list;
    private ViewPager view_pager2;
    private List<ImageView> imageViews = new ArrayList<ImageView>();
    private String api = "http://gank.io/api/data/all/20/2";
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0){
                String json = (String) msg.obj;
                try {
                    JSONObject jsonObject = new JSONObject(json);
                    JSONArray banner = jsonObject.getJSONArray("banner");
                    for (int i = 0 ; i < banner.length();i++){
                        JSONObject jo = banner.getJSONObject(i);
                        String image_url = jo.getString("image_url");
                        ImageView imageView = new ImageView(getContext());
                        ImageLoader.getInstance().displayImage(image_url,imageView);
                        imageViews.add(imageView);
                    }
                    MyPagerAdapter adapter = new MyPagerAdapter(imageViews, getContext());
                    view_pager2.setAdapter(adapter);
                    sendEmptyMessageDelayed(9,2000);
                } catch (Exception e) {
                    e.printStackTrace();

                }
            }
            if (msg.what==9){
                view_pager2.setCurrentItem(view_pager2.getCurrentItem()+1);
                sendEmptyMessageDelayed(9,2000);

            }
        }
    };

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment1_layout, container, false);
        x_list = inflate.findViewById(R.id.x_list);
        view_pager2 = inflate.findViewById(R.id.view_pager2);
        return inflate;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        //创建方法
        getImage();
        new getData().execute(api);
    }

    private void getImage() {
        final String path = "http://result.eolinker.com/iYXEPGn4e9c6dafce6e5cdd23287d2bb136ee7e9194d3e9?uri=banner";
        new Thread(){
            @Override
            public void run() {
                super.run();
                try {
                    URL url = new URL(path);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("GET");
                    connection.setConnectTimeout(5000);
                    connection.setReadTimeout(5000);
                    if (connection.getResponseCode()==200){
                        InputStream inputStream = connection.getInputStream();
                        ByteArrayOutputStream bos = new ByteArrayOutputStream();
                        byte[] bytes = new byte[1024];
                        int len = 0 ;
                        while ((len = inputStream.read(bytes))!=-1){
                                bos.write(bytes,0,len);
                        }
                        bos.close();
                        inputStream.close();
                        String json = bos.toString();
                        Message message = handler.obtainMessage();
                        message.what = 0;
                        message.obj = json;
                        message.sendToTarget();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    private class getData extends AsyncTask<String,Integer,String >{
        @Override
        protected String doInBackground(String... strings) {
            try {
                URL url = new URL(strings[0]);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setReadTimeout(5000);
                connection.setConnectTimeout(5000);
                connection.setRequestMethod("GET");
                if (connection.getResponseCode()==200){
                    return StringStream(connection.getInputStream());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        private String StringStream(InputStream inputStream) {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] bytes = new byte[1024];
            int len = 0;
            try {
                while ((len = inputStream.read(bytes))!=-1){
                    bos.write(bytes,0,len);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return new String(bos.toByteArray());
        }

        //写解析
        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            Gson gson = new Gson();
            DataInfo dataInfo = gson.fromJson(s, DataInfo.class);
            //创建适配器
            MyListAdapter adapter = new MyListAdapter(dataInfo.getResults(), getContext());
            x_list.setAdapter(adapter);
        }
    }
}


















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值