您的位置:首页 > 移动开发 > Android开发

Android 安卓原生Json解析并展示listview

2017-12-18 21:11 375 查看
MainActivity

import android.os.Handler;

import android.os.Message;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.util.Log;

import android.widget.ListView;

import org.json.JSONArray;

import org.json.JSONException;

import org.json.JSONObject;

import java.io.InputStream;

import java.net.HttpURLConnection;

import java.net.MalformedURLException;

import java.net.URL;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

/*

* http://www.moviebase.cn/uread/app/topic/topicList?platform=2&deviceId=A8C73E0D1ED1A2BED491C9238C8FD8A0&channelId=1003&pageContext=1
*/

public class MainActivity extends AppCompatActivity {

    ListView lv;

    List<Map<String,String>> list;

    AAAdapter aaAdapter;

    Handler handler=new Handler(){

        @Override

        public void handleMessage(Message msg) {

            super.handleMessage(msg);

            if (msg.what==270){

                String json= (String) msg.obj;

                try {

                    JSONObject object=new JSONObject(json);

                    JSONArray been=object.getJSONArray("topicList");

                    for (int i=0;i<been.length();i++){

                        JSONObject TopicListBean=been.getJSONObject(i);

                        String title=TopicListBean.getString("title");

                        String desc=TopicListBean.getString("desc");

                        Map<String,String> map=new HashMap<>();

                        map.put("title",title);

                        map.put("desc",desc);

                        list.add(map);

                    }

                    aaAdapter.notifyDataSetChanged();

                } catch (JSONException e) {

                    e.printStackTrace();

                }

            }

        }

    };

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        lv= (ListView) findViewById(R.id.lv);

        list=new ArrayList<>();

        aaAdapter=new AAAdapter(this,list);

        lv.setAdapter(aaAdapter);

        new os().start();

    }

    class os extends Thread{

        String urla="http://www.moviebase.cn/uread/app/topic/topicList?platform=2&deviceId=A8C73E0D1ED1A2BED491C9238C8FD8A0&channelId=1003&pageContext=1";

        @Override

        public void run() {

            super.run();

            try {

                URL url=new URL(urla);

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

                conn.setConnectTimeout(5000);

                conn.connect();

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

                    InputStream is = conn.getInputStream();

                    byte[] bytes = new byte[1024];

                    int i = 0 ;

                    StringBuffer sb = new StringBuffer();

                    while ((i=is.read(bytes))!=-1) {

                        sb.append(new String(bytes, 0, i));

                    }

                    Message message = new Message();

                    message.what=270;

                    message.obj=sb.toString();

                    handler.sendMessage(message);

                }

            } catch (Exception e) {

                e.printStackTrace();

            }

        }

    }
}

AA -----自己打

AAAdapter

package com.example.myapplication;

import android.content.Context;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.BaseAdapter;

import android.widget.TextView;

import java.util.List;

import java.util.Map;

/**

 * Created by 猴嫂 on 2017/12/16.

 */

public class AAAdapter extends BaseAdapter {

    private List<Map<String,String>> list;

    private Context context;

    public AAAdapter(Context context, List<Map<String, String>> list) {

        this.context = context;

        this.list = list;

    }

    @Override

    public int getCount() {

        return list.size();

    }

    @Override

    public Object getItem(int position) {

        return list.get(position);

    }

    @Override

    public long getItemId(int position) {

        return position;

    }

    @Override

    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder vh=null;

        if (convertView==null){

            convertView= LayoutInflater.from(context).inflate(R.layout.list_aa,null);

            vh=new ViewHolder(convertView);

            vh.tv_title= (TextView) convertView.findViewById(R.id.tv_title);

            vh.tv_desc= (TextView) convertView.findViewById(R.id.tv_desc);

            convertView.setTag(vh);

        }else{

            vh= (ViewHolder) convertView.getTag();

        }

        Map<String,String> aa=list.get(position);

        vh.tv_title.setText(aa.get("title"));

        vh.tv_desc.setText(aa.get("desc"));

        return convertView;

    }

    class ViewHolder{

        TextView tv_title;

        TextView tv_desc;

        public ViewHolder(View view){

            tv_title= (TextView) view.findViewById(R.id.tv_title);

            tv_desc= (TextView) view.findViewById(R.id.tv_desc);

        }

    }

}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/activity_main"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context="com.example.myapplication.MainActivity">

    <ListView

        android:id="@+id/lv"

        android:layout_width="wrap_content"

        android:layout_height="match_parent"></ListView>

</LinearLayout>

list_aa.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">

    <TextView

        android:id="@+id/tv_title"

        android:text="兔八哥聊电影"

        android:textSize="16dp"

        android:textColor="#000"

        android:layout_marginLeft="20dp"

        android:layout_marginTop="10dp"

        android:layout_width="match_parent"

        android:layout_height="wrap_content" />

    <TextView

        android:id="@+id/tv_desc"

        android:text="用第一人称的方式吐槽电影"

        android:textSize="16dp"

        android:textColor="#474747"

        android:layout_marginLeft="20dp"

        android:layout_marginTop="10dp"

        android:layout_width="match_parent"

        android:layout_height="wrap_content" />

</LinearLayout>

<uses-permission android:name="android.permission.INTERNET"/>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  it