网络请求httpurlconnection ,异步加载 放入viewpage中

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/LIUHAIFENG99999/article/details/51736498
package com.bwei.viewpagedemo;

import android.os.AsyncTask;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.google.gson.Gson;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.net.URLEncoder;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ViewPager viewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewPager = (ViewPager) findViewById(R.id.viewpage);

        qqwl();

    }

    public void qqwl() {

        new AsyncTask<Void, Void, String>() {
            @Override
            protected String doInBackground(Void... params) {

                String json = initData();

                return json;
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);

                Gson gson = new Gson();

                Bean bean = gson.fromJson(s, Bean.class);

                List<Bean.ResultBean.VideoRecBean> video_rec = bean.getResult().getVideo_rec();

                viewPager.setAdapter(new MyPageAdapter(video_rec, MainActivity.this));

            }
        }.execute();


    }

    public String initData() {

        String conn = null;
        try {
         //   String str="http://op.juhe.cn/onebox/movie/video?key=8deaf6b2abcafd75da71fb10e08954e8&q=好先生";

            URL url = new URL("http://op.juhe.cn/onebox/movie/video?key=8deaf6b2abcafd75da71fb10e08954e8&q="+ URLEncoder.encode("好先生","utf-8"));

            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            connection.setRequestMethod("GET");

            connection.connect();//连接

            if (connection.getResponseCode() == 200) {

                BufferedReader bf = new BufferedReader(new InputStreamReader(connection.getInputStream(),"utf-8"));

                while ((conn = bf.readLine()) != null) {
                    System.out.println(conn);
                    return conn;
                }


            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;

    }


}





package com.bwei.viewpagedemo;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.lidroid.xutils.BitmapUtils;

import java.util.List;

/**
 * Created by Administrator on 2016/6/21.
 */
public class MyPageAdapter extends PagerAdapter {

    private List<Bean.ResultBean.VideoRecBean> video_rec;

    private Context context;

    public MyPageAdapter(List<Bean.ResultBean.VideoRecBean> video_rec, Context context) {
        this.video_rec = video_rec;
        this.context = context;
    }

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

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view==object;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View)object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        View view=View.inflate(context,R.layout.item,null);

        ImageView imageview= (ImageView) view.findViewById(R.id.image);

        BitmapUtils  bitmapUtils=new BitmapUtils(context);

        bitmapUtils.display(imageview,video_rec.get(position).getCover());

        container.addView(view);

        return view;
    }
}



展开阅读全文

没有更多推荐了,返回首页