获取网络数据

    compile 'org.greenrobot:eventbus:3.0.0'
    compile'org.greenrobot:greendao:3.0.1'
    compile'org.greenrobot:greendao-generator:3.0.0'
    compile 'com.android.support:recyclerview-v7:26.0.0-alpha1'
    compile 'com.squareup.retrofit2:retrofit:2.1.0'
    compile 'com.squareup.retrofit2:converter-gson:2.1.0'
    compile 'com.facebook.fresco:fresco:0.11.0'
 android:name=".utils.MyApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"

MyApplication

 @Override
    public void onCreate() {
        super.onCreate();
        Fresco.initialize(this);


    }

MainActivity

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    List list=new ArrayList<>();
    private  TextView[] tvb;
    private ViewPager vp;
    private boolean flag;

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



         vp= (ViewPager) findViewById(R.id.viewpge);
      TextView tv1= (TextView) findViewById(R.id.tex0);
        tv1.setOnClickListener(this);
        TextView tv2= (TextView) findViewById(R.id.tex1);
        tv2.setOnClickListener(this);
        TextView tv3= (TextView) findViewById(R.id.tex2);
        tv3.setOnClickListener(this);
        TextView tv4= (TextView) findViewById(R.id.tex3);
        tv4.setOnClickListener(this);
        TextView tv5= (TextView) findViewById(R.id.tex4);
        tv5.setOnClickListener(this);
         tvb=new TextView[]{tv1,tv2,tv3,tv4,tv5};

        list.add(new OneFragment());
        list.add(new TwoFragment());
        list.add(new ThreeFragment());
        list.add(new fourFragment());
        list.add(new FiveFragment());
        if (isOnline()) {
            GetData(0);
        }else{
          showDialog();
        }


        vp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return list.get(position);
            }

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

        vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                GetData(position);
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        GetData(0);




    }

    private void showDialog(){

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("无网络连接");
        builder.setNegativeButton("关闭",null);


        builder.setPositiveButton("转到网络设置", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //设置跳转网络界面
                startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
            }
        });

        builder.create().show();

    }
    public boolean isOnline() {
        //得到网络连接的管理者
        ConnectivityManager connMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
        //通过网络管理者得到网络信息
        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
        //判断网络是否连接
        return (networkInfo != null && networkInfo.isConnected());
    }

    public void GetData(int porsion){
        for(int i=0;iif (porsion==i){
                    tvb[i].setTextColor(Color.BLUE);
                }else{
                    tvb[i].setTextColor(Color.BLACK);
                }
        }
    }

    /**
     * Called when a view has been clicked.
     *
     * @param v The view that was clicked.
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tex0:
                vp.setCurrentItem(0);
                GetData(0);

                break;
            case R.id.tex1:
                vp.setCurrentItem(1);
                GetData(1);

                break;
            case R.id.tex2:
                vp.setCurrentItem(2);
                GetData(2);

                break;
            case R.id.tex3:
                vp.setCurrentItem(3);
                GetData(3);

                break;
            case R.id.tex4:
                vp.setCurrentItem(4);
                GetData(04);

                break;
        }

    }

}

UtilsURL

 public static final String BASE_HOST_URL="******";
    public  static final String TAGS="*****";

ServiceAPI

public interface ServiceAPI {
    @GET(UtilsURL.TAGS)
    Call bean();
}

Myadadap

public class Myadadap extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private List list=new ArrayList<>();
    private Context context;

    public Myadadap(List list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflat= LayoutInflater.from(context).inflate(R.layout.bj_item,parent,false);
        return new sViewHodler(inflat);
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        sViewHodler s= (sViewHodler) holder;
        Uri uri=Uri.parse("http://img4q.duitang.com/uploads/item/201411/20/20141120132318_3eAuc.thumb.700_0.jpeg");
        s.sim.setImageURI(uri);
        s.tv2.setText(list.get(position).getPublishedAt());
        s.tv1.setText(list.get(position).getDesc());
    }


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

    private class sViewHodler extends RecyclerView.ViewHolder{
private SimpleDraweeView sim;
        private TextView tv1;
        private TextView tv2;
        public sViewHodler(View itemView) {
            super(itemView);
            sim=itemView.findViewById(R.id.recee);
            tv1=itemView.findViewById(R.id.tv1);
            tv2=itemView.findViewById(R.id.tv2);
        }
    }
}

OneFragment

public class OneFragment extends Fragment{
    private View view;
    private RecyclerView rec;


    private List  list=new ArrayList<>();
    private UserDao mUserDao;
    private User mUser;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
         view=inflater.inflate(R.layout.item2,null);
//        EventBus.getDefault().register(getContext());
        init();

        Getdata();
        return view;
    }




    private void init() {
      rec= view.findViewById(R.id.rde);
        LinearLayoutManager lin=new LinearLayoutManager(getActivity());
        rec.setLayoutManager(lin);
    }

    private void Getdata() {

        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(UtilsURL.BASE_HOST_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        ServiceAPI serviceAPI = retrofit.create(ServiceAPI.class);
        Call bean = serviceAPI.bean();
        bean.enqueue(new Callback() {
            @Override
            public void onResponse(Call call, Response response) {
                List results = response.body().getResults();
                Myadadap myadadap=new Myadadap(results,getActivity());
                rec.setAdapter(myadadap);
                mUserDao = MyApplication.getInstances().getDaoSession().getUserDao();
                for (int i = 0; i < results.size(); i++) {
                    mUser = new User(null,results.get(i).getDesc());
                    mUserDao.insert(mUser);//添加一个
                }
            }

            @Override
            public void onFailure(Call call, Throwable t) {

            }
        });
    }

你可能感兴趣的:(获取网络数据)