網絡解析多接口

Fragment_Home

 MyPagerAdapter mypageradapter;
    ViewPager viewpager;
    List<CommendClass> list;
    View view, view1, view2, view3, view4, view5;

    TextView home_tv1, home_tv2, home_tv3, home_tv4, home_tv5;
    ImageView home_iv;

    Home_bean bean;
//    Handler handler = new Handler() {
//        @Override
//        public void handleMessage(Message msg) {
//            super.handleMessage(msg);
//            {
//                bean = (Home_bean) msg.obj;
//
//                home_tv1 = (TextView) view1.findViewById(R.id.main_hp_title);
//                home_tv2 = (TextView) view1.findViewById(R.id.main_tv_hp_content);
//                home_tv3 = (TextView) view1.findViewById(R.id.main_tv_praisenum);
//                home_tv4 = (TextView) view1.findViewById(R.id.main_hp_author);
//                home_tv5 = (TextView) view1.findViewById(R.id.main_tv_hp_makettime);
//
//                home_iv = (ImageView) view1.findViewById(R.id.main_iv_big);
////                Glide.with(getActivity()).load(bean.data.hp_img_url).into( home_iv);
//                home_tv1.setText(bean.data.hp_title);
//                home_tv2.setText(bean.data.hp_content);
//                home_tv3.setText(bean.data.praisenum);
//                home_tv4.setText(bean.data.hp_author);
//                home_tv5.setText(bean.data.hp_makettime);
//                initTen();
//            }
//        }
//    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_home, null);

        return view;
    }

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

        initData();
    }


    private void initData() {
        list = new ArrayList<>();

        viewpager = (ViewPager) view.findViewById(R.id.fragment_home_viewpager);

        for (int i = 0; i < 4; i++) {
            CommendClass pager = new CommendClass(getActivity(), i);
            list.add(pager);
        }
        MyAdapter adapter = new MyAdapter();
        viewpager.setAdapter(adapter);

    }

    class MyAdapter extends PagerAdapter {

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

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

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View view = list.get(position).initView();
            container.addView(view);
            list.get(position).initData();
            return view;
        }

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


//    public void Eight() {
//
//        new Thread() {
//            @Override
//            public void run() {
//                super.run();
//                String result = HTTPGet.GetURL("http://v3.wufazhuce.com:8000/api/hp/detail/1426");
//                bean = parseHson(result);
//
//                Message message = new Message();
//                message.obj = bean;
//                handler.sendMessage(message);
//            }
//        }.start();
//    }

//    public Home_bean parseHson(String json) {
//        try {
//            JSONObject object = new JSONObject(json);
//            bean = new Home_bean();
//            bean.res = object.getInt("res");
//            JSONObject objectdata = object.getJSONObject("data");
//            bean.data = bean.new Data1();
//            bean.data.hp_author = objectdata.getString("hp_author");
//            bean.data.hp_content = objectdata.getString("hp_content");
//            bean.data.hp_img_url = objectdata.getString("hp_img_url");
//            Log.e("bitmap", "run: " + bean.data.hp_img_url);
//            bean.data.hp_makettime = objectdata.getString("hp_makettime");
//            bean.data.hp_title = objectdata.getString("hp_title");
//            bean.data.praisenum = objectdata.getString("praisenum");
//            bean.data.sharenum = objectdata.getInt("sharenum");
//            bean.data.hpcontent_id = objectdata.getInt("hpcontent_id");
//            return bean;
//        } catch (JSONException e) {
//            e.printStackTrace();
//        }
//        return null;
//    }

//    public void initTen() {
//
//        new Thread() {
//            @Override
//            public void run() {
//                super.run();
//                Log.e("initTen", "run: " + bean.data.hp_img_url);
//                final Bitmap bitmap = HTTPGet.httpGetBitMap(bean.data.hp_img_url);
//                home_iv.post(new Runnable() {
//                    @Override
//                    public void run() {
//                        home_iv.setImageBitmap(bitmap);
//                    }
//                });
//            }
//        }.start();
//    }

}

CommendClass

package com.baozilichao.superone.Utils;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.baozilichao.superone.R;

/**
 * Created by user on 2016/8/18.
 */
public class CommendClass {
    Context context;
    View view;
    TextView home_tv1, home_tv2, home_tv3, home_tv4, home_tv5;
    ImageView home_iv1;
    int num;



    public CommendClass(Context context,int num) {
        this.context = context;
        this.num=num;
    }
    public View initView(){
        view = LayoutInflater.from(context).inflate(R.layout.home_viewpager_one,null);
        return view;
    }
    public void initData(){
        home_tv1 = (TextView) view.findViewById(R.id.main_hp_title);
        home_tv2 = (TextView) view.findViewById(R.id.main_tv_hp_content);
        home_tv3 = (TextView) view.findViewById(R.id.main_tv_praisenum);
        home_tv4 = (TextView) view.findViewById(R.id.main_hp_author);
        home_tv5 = (TextView) view.findViewById(R.id.main_tv_hp_makettime);
        home_iv1= (ImageView) view.findViewById(R.id.main_iv_big);
        new CommendHomeAsyncTask(num,home_tv1, home_tv2, home_tv3, home_tv4, home_tv5,home_iv1).execute();

    }
}

CommendHomeAsyncTask

package com.baozilichao.superone.Utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import com.baozilichao.superone.bean.Home_bean;
import com.baozilichao.superone.bean.Read_bean;
import com.baozilichao.superone.httpgeturl.HTTPGet;

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

/**
 * Created by user on 2016/8/18.
 */
public class CommendHomeAsyncTask extends AsyncTask<String, String, Home_bean> {
    String result;
    int num;
    Home_bean bean2;
    TextView home_tv1, home_tv2, home_tv3, home_tv4, home_tv5;
    ImageView home_iv1;
    Bitmap bitmap;

    public CommendHomeAsyncTask(int num, TextView home_tv1, TextView home_tv2, TextView home_tv3, TextView home_tv4, TextView home_tv5, ImageView home_iv1) {
        this.num = num;
        this.home_tv1 = home_tv1;
        this.home_tv2 = home_tv2;
        this.home_tv3 = home_tv3;
        this.home_tv4 = home_tv4;
        this.home_tv5 = home_tv5;
        this.home_iv1=home_iv1;
    }

    @Override
    protected Home_bean doInBackground(String... strings) {
        switch (num) {
            case 0:
                result = HTTPGet.GetURL("http://v3.wufazhuce.com:8000/api/hp/detail/1426");
                bean2 = ParesHason(result);
                break;
            case  1:
                result=HTTPGet.GetURL("http://v3.wufazhuce.com:8000/api/hp/detail/1422");
                bean2 = ParesHason(result);
                break;
            case  2:
                result=HTTPGet.GetURL("http://v3.wufazhuce.com:8000/api/hp/detail/1423");
                bean2 = ParesHason(result);
                break;
            case  3:
                result=HTTPGet.GetURL("http://v3.wufazhuce.com:8000/api/hp/detail/1418");
                bean2 = ParesHason(result);
                break;




        }
        bitmap=HTTPGet.httpGetBitMap(bean2.data.hp_img_url);
        return bean2;
    }

    @Override
    protected void onPostExecute(Home_bean home_bean) {
        super.onPostExecute(home_bean);
        home_tv1.setText(bean2.data.hp_title);
        home_tv2.setText(bean2.data.hp_content);
        home_tv3.setText(bean2.data.praisenum);
        home_tv4.setText(bean2.data.hp_author);
        home_tv5.setText(bean2.data.hp_makettime);
        home_iv1.setImageBitmap(bitmap);


    }

    private Home_bean ParesHason(String json) {
        try {
            JSONObject object = new JSONObject(json);
            Home_bean bean = new Home_bean();
            JSONObject objectdata = object.getJSONObject("data");
            bean.data = bean.new Data1();
            bean.data.hp_author = objectdata.getString("hp_author");
            bean.data.hp_content = objectdata.getString("hp_content");
            bean.data.hp_img_url = objectdata.getString("hp_img_url");
            Log.e("bitmap", "run: " + bean.data.hp_img_url);
            bean.data.hp_makettime = objectdata.getString("hp_makettime");
            bean.data.hp_title = objectdata.getString("hp_title");
            bean.data.praisenum = objectdata.getString("praisenum");
            bean.data.sharenum = objectdata.getInt("sharenum");
            bean.data.hpcontent_id = objectdata.getInt("hpcontent_id");
            return bean;
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }
}

Home_bean

package com.baozilichao.superone.bean;

/**
 * Created by user on 2016/8/15.
 */
public class  Home_bean {
    public int res;
    public  Data1 data;
    public class Data1{
        public int author_id;
        public String hp_author;
        public String hp_content;
        public String hp_img_url;
        public String hp_makettime;
        public String hp_title;
        public String praisenum;
        public int sharenum;
        public int hpcontent_id;

    }
}

home_viewpager_one.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ScrollView
        android:id="@+id/main_scollview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/main_linearlayout2"
        android:layout_below="@+id/main_linearlayout1">

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="10dp">

            <RelativeLayout
                android:id="@+id/main_relativelayout1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/main_line1">


                <ImageView
                    android:id="@+id/main_iv_big"
                    android:layout_width="match_parent"
                    android:layout_height="250dp"
                    android:layout_margin="8dp"
                    android:layout_alignParentTop="true"
                    android:scaleType="fitXY"
                   />

                <TextView
                    android:id="@+id/main_hp_title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignLeft="@+id/main_iv_big"
                    android:layout_below="@+id/main_iv_big"
                    android:textSize="10sp" />

                <TextView
                    android:id="@+id/main_hp_author"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignRight="@+id/main_iv_big"
                    android:layout_below="@+id/main_iv_big"
                    android:textSize="10sp" />

                <TextView
                    android:id="@+id/main_tv_hp_content"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignLeft="@+id/main_iv_big"
                    android:layout_alignRight="@+id/main_iv_big"
                    android:layout_below="@+id/main_hp_title"
                    android:layout_marginTop="20dp"
                    android:textColor="#3E3E3E"
                    android:textSize="12sp" />

                <TextView
                    android:id="@+id/main_tv_hp_makettime"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignRight="@+id/main_iv_big"
                    android:layout_below="@+id/main_tv_hp_content"
                    android:layout_marginBottom="8dp"
                    android:layout_marginTop="50dp"
                    android:text="Thu 04 Aug.2016"
                    android:textSize="11sp" />

                <TextView
                    android:id="@+id/main_tv_no_use1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignTop="@+id/main_tv_hp_makettime"
                    android:layout_marginRight="10dp"
                    android:layout_toLeftOf="@id/main_tv_hp_makettime"
                    android:text="@string/earth"
                    android:textSize="11sp" />

                <ImageView
                    android:id="@+id/main_iv_weather"
                    android:layout_width="20dp"
                    android:layout_height="20dp"
                    android:layout_alignTop="@+id/main_tv_no_use1"
                    android:layout_marginRight="10dp"
                    android:layout_toLeftOf="@+id/main_tv_no_use1"
                    android:background="@mipmap/weather_sunny" />

            </RelativeLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="45dp"
                android:layout_alignLeft="@+id/main_relativelayout1"
                android:layout_alignRight="@+id/main_relativelayout1"
                android:layout_below="@+id/main_relativelayout1"
                android:orientation="horizontal">

                <CheckBox
                    android:id="@+id/main_iv_no_use2"
                    android:layout_width="35dp"
                    android:layout_height="35dp"
                    android:background="@drawable/diary_click"
                    android:button="@null" />

                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_toRightOf="@+id/main_iv_no_use2"
                    android:layout_weight="1"
                    android:paddingTop="10dp"
                    android:text="@string/diary"
                    android:textSize="10sp" />

                <CheckBox
                    android:layout_width="35dp"
                    android:layout_height="35dp"
                    android:background="@drawable/laud_click"
                    android:button="@null" />

                <TextView
                    android:id="@+id/main_tv_praisenum"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.5"
                    android:paddingTop="10dp"
                    android:text="10808"
                    android:textSize="10sp" />
                <ImageView
                    android:layout_width="35dp"
                    android:layout_height="35dp"
                    android:paddingRight="0dp"
                    android:src="@drawable/share_image" />
            </LinearLayout>
        </RelativeLayout>
    </ScrollView>

</LinearLayout>

fragment_home.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activity.MainActivity">
    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/fragment_home_viewpager"/>


</RelativeLayout>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章