JsonObject和JsonArray解析

public class MainActivity extends Activity {

    private ViewPager vp;
    private PullToRefreshListView lv;
    private String path="http://c.m.163.com/nc/article/headline/T1348647909107/0-20.html";
    private List<String> mVPlist;
    private List<MyBean> mlist;

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

        vp = (ViewPager) findViewById(R.id.vp);
        lv = (PullToRefreshListView) findViewById(R.id.listview);

        initData();
    }

    private void initData() {
        new AsyncTask<Void, Void, String>() {

            @Override
            protected String doInBackground(Void... params) {
                try {
                    URL  url =new URL(path);

                    HttpURLConnection conn =(HttpURLConnection) url.openConnection();
                    conn.setRequestMethod("GET");
                    conn.setConnectTimeout(5000);
                    if (conn.getResponseCode()==200) {
                        InputStream inputStream = conn.getInputStream();
                        BufferedReader br =new BufferedReader(new InputStreamReader(inputStream));
                        StringBuffer sf =new StringBuffer();
                        String str = "";
                        while ((str = br.readLine())!=null) {
                            sf.append(str);
                        }
                        return sf.toString();
                    }

                } catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onPostExecute(String result) {
                super.onPostExecute(result);
//              Log.e("TAG", result);
                //解析json串
                JsonToString(result);
            }

        }.execute();

    }

    private void JsonToString(String result) {
        mVPlist = new ArrayList<String>();
        mlist = new ArrayList<MyBean>();
        try {
            JSONObject  jo =new JSONObject(result);
            JSONArray array = jo.optJSONArray("T1348647909107");
            for (int i = 0; i < array.length(); i++) {
                MyBean b = new MyBean();
                JSONObject obj = array.optJSONObject(i);
                JSONArray ads = obj.optJSONArray("ads");
                if (ads!=null) {
                    for (int j = 0; j < ads.length(); j++) {
                        JSONObject oo = ads.optJSONObject(j);
                        String img = oo.optString("imgsrc");
                        mVPlist.add(img);
                    }
                }
                String title = obj.optString("title");
                if (title!=null) {
                    b.title=title;
                }

                String content = obj.optString("digest");
                if (content!=null) {
                    b.content = content;
                }

                String img = obj.optString("imgsrc");
                if (img!=null) {
                    b.img = img;
                }

                JSONArray imgextra = obj.optJSONArray("imgextra");
                if (imgextra!=null) {
                    b.my=new ArrayList<MyBean.My>();
                    for (int j = 0; j < imgextra.length(); j++) {
                        JSONObject oo = imgextra.optJSONObject(j);
                        String imgsrc = oo.optString("imgsrc");
                        Log.e("----------", imgsrc);
                        MyBean.My m=new MyBean.My();
                        m.imgs=imgsrc;
                        b.my.add(m);
                    }
                }

                mlist.add(b);

            }

            vp.setAdapter(new VpAdapter());
            lv.setAdapter(new MyListAdaoter());

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    class VpAdapter extends PagerAdapter{

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return mVPlist.size();
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            // TODO Auto-generated method stub
            return arg0==arg1;
        }

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

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ImageView  img =new ImageView(MainActivity.this);
            img.setScaleType(ScaleType.FIT_XY);
            ImageLoader.getInstance().displayImage(mVPlist.get(position), img);

            container.addView(img);
            return img;
        }

    }

    class MyListAdaoter extends BaseAdapter{

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return mlist.size();
        }

        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return mlist.get(arg0);
        }

        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return arg0;
        }
        @Override
        public int getViewTypeCount() {
            // TODO Auto-generated method stub
            return 2;
        }
        @Override
        public int getItemViewType(int position) {
            if (mlist.get(position).my!=null) {
                return 0;
            }else{
                return 1;
            }

        }

        @Override
        public View getView(int position, View v, ViewGroup arg2) {
            int type = getItemViewType(position);
            List<ImageView> imgs =  new ArrayList<ImageView>();
            ViewHolder holer;
            switch (type) {
            case 1:
                if (v==null) {
                    holer = new ViewHolder();
                    v =  View.inflate(MainActivity.this, R.layout.item1, null);
                    holer. img  = (ImageView) v.findViewById(R.id.img);
                    holer. t1 = (TextView) v.findViewById(R.id.title);
                    holer. t2 = (TextView) v.findViewById(R.id.content);
                    v.setTag(holer);
                }else{
                    holer = (ViewHolder) v.getTag();
                    ImageLoader.getInstance().displayImage(mlist.get(position).img, holer.img);
                    holer. t1.setText(mlist.get(position).title);
                    holer. t2.setText(mlist.get(position).content);
                }
                break;
            case 0:
                if (v==null) {
                    holer = new ViewHolder();
                    v =  View.inflate(MainActivity.this, R.layout.item2, null);
                    LinearLayout lin = (LinearLayout) v.findViewById(R.id.liner);

                    holer. t1 = (TextView) v.findViewById(R.id.title);
                    if (mlist.get(position).my!=null) {
                        Log.e("TAG--",  mlist.get(position).my.size()+"-------");
                        List<My> my = mlist.get(position).my;
                        Log.e("xiaou",my.toString());
                        for (int i = 0; i < mlist.get(position).my.size(); i++) {
                            holer. img = new ImageView(MainActivity.this);
                            holer. img.setScaleType(ScaleType.FIT_XY);
                            LayoutParams p = new LayoutParams(50, 50);
                            p.setMargins(10, 0, 10, 0);
                            lin.addView(holer. img,p);
                            //imgs.add(holer. img);
                            ImageLoader.getInstance().displayImage(my.get(i).imgs, holer.img);
                        }
                    }


                    v.setTag(holer);
                }else{
                    holer = (ViewHolder) v.getTag();
                    holer. t1.setText(mlist.get(position).title);
                    List<My> my = mlist.get(position).my;
                    Log.e("xiaou",my.toString());

//                  if (my!=null) {

                        for (int i = 0; i < imgs.size(); i++) {
                            Log.e("TAG++",  "-------"+my.get(i).imgs);
                            ImageLoader.getInstance().displayImage(my.get(i).imgs, imgs.get(i));
                        }
//                  }

                }
                break;

            default:
                break;
            }
            return v;
        }

    }

    class ViewHolder{
        TextView t1,t2;
        ImageView img;
    }

}

bean包

public class MyBean {

    public String title;
    public String content;
    public String img;
    public List<My> my;

    public static class My {
        public String imgs;
    }
}

布局main

<LinearLayout 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"
    android:orientation="vertical" >

    <android.support.v4.view.ViewPager
        android:id="@+id/vp"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        ></android.support.v4.view.ViewPager>

    <com.handmark.pulltorefresh.library.PullToRefreshListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
</com.handmark.pulltorefresh.library.PullToRefreshListView>

</LinearLayout>

item1

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <ImageView 
        android:id="@+id/img"
        android:layout_height="50dp"
        android:layout_width="50dp"
        android:src="@drawable/ic_launcher"/>
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView 
            android:id="@+id/title"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="标题"
            android:textSize="20sp"/>
        <TextView 
            android:id="@+id/content"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="内容"
            android:textSize="20sp"/>
    </LinearLayout>

</LinearLayout>

item2

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

     <TextView 
            android:id="@+id/title"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="标题"
            android:textSize="20sp"/>
    <LinearLayout 
        android:id="@+id/liner"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:orientation="horizontal">

    </LinearLayout>

</LinearLayout>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值