解析JSON最外层是集合------->[]

package com.example.text;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
 
import com.loopj.android.image.SmartImageView;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.annotation.SuppressLint;
import android.app.Activity;

public class MainActivity extends Activity {
    private List<Bean> bean;
    private ListView lv;
@SuppressLint("HandlerLeak")
private Handler handler=new Handler(){
    
    

    @SuppressWarnings("unchecked")
    public void handleMessage(android.os.Message msg) {
        bean = (List<Bean>) msg.obj;
        lv.setAdapter(new Myadapter());
        
    }
    
    ;
};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        
        lv=(ListView) findViewById(R.id.listView1);
        new Thread() {
            

            public void run() {
                String path = "http://172.17.29.120/localuser/ljy/datas/data.json";
                // 创建HttpClient对象
                HttpClient httpClient = new DefaultHttpClient();
                // 创建请求方式
                HttpGet httpget = new HttpGet(path);
                try {
                    HttpResponse httpResponse = httpClient.execute(httpget);

                    HttpEntity entity = httpResponse.getEntity();
                    String string = EntityUtils.toString(entity, "gbk");
                    //得到JSONArray对象
                    JSONArray ja = new JSONArray(string);
                    //创建一个集合
                    List<Bean>     list = new ArrayList<Bean>();
                   //循环解析ja 添加到集合中
                    for (int i = 0; i < ja.length(); i++) {
                        JSONObject jo = ja.optJSONObject(i);
                        list.add(new Bean(jo.getString("intro"),
                                jo.getString("pic"), jo.getString("title")));

                        
                    }
            Message msg=Message.obtain();
            msg.obj=list;
            handler.sendMessage(msg);
                

                } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                // 判断,并解析
                catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            };
        }.start();

    }

    // public static ArrayList<Bean> getBeanl(String json){
    //
    // return gson.fromJson(json, new TypeToken<List<Bean>>(){}.getType());
    // }

    
    class Myadapter extends BaseAdapter{

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

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(int position, View v, ViewGroup parent) {
            ViewHolder holder;
            if (v == null) {
                holder = new ViewHolder();
                v = LayoutInflater.from(MainActivity.this).inflate(R.layout.list_item, null);
                holder.content = (TextView) v.findViewById(R.id.content);
                holder.img = (SmartImageView) v.findViewById(R.id.img);
                holder.title = (TextView) v.findViewById(R.id.title);
                v.setTag(holder);
            } else {
                holder = (ViewHolder) v.getTag();
            }
            holder.img.setImageUrl(bean.get(position).getPic());
            holder.title.setText(bean.get(position).getTitle());
            holder.content.setText(bean.get(position).getIntro());
            return v;
        }
        
        
    }
    
    class ViewHolder {
        private SmartImageView img;
        private TextView title;
        private TextView content;
    }
}

package com.example.text;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
 
import com.loopj.android.image.SmartImageView;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.annotation.SuppressLint;
import android.app.Activity;

public class MainActivity extends Activity {
    private List<Bean> bean;
    private ListView lv;
@SuppressLint("HandlerLeak")
private Handler handler=new Handler(){
    
    

    @SuppressWarnings("unchecked")
    public void handleMessage(android.os.Message msg) {
        bean = (List<Bean>) msg.obj;
        lv.setAdapter(new Myadapter());
        
    }
    
    ;
};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        
        lv=(ListView) findViewById(R.id.listView1);
        new Thread() {
            

            public void run() {
                String path = "http://172.17.29.120/localuser/ljy/datas/data.json";
                // 创建HttpClient对象
                HttpClient httpClient = new DefaultHttpClient();
                // 创建请求方式
                HttpGet httpget = new HttpGet(path);
                try {
                    HttpResponse httpResponse = httpClient.execute(httpget);

                    HttpEntity entity = httpResponse.getEntity();
                    String string = EntityUtils.toString(entity, "gbk");
                    //得到JSONArray对象
                    JSONArray ja = new JSONArray(string);
                    //创建一个集合
                    List<Bean>     list = new ArrayList<Bean>();
                   //循环解析ja 添加到集合中
                    for (int i = 0; i < ja.length(); i++) {
                        JSONObject jo = ja.optJSONObject(i);
                        list.add(new Bean(jo.getString("intro"),
                                jo.getString("pic"), jo.getString("title")));

                        
                    }
            Message msg=Message.obtain();
            msg.obj=list;
            handler.sendMessage(msg);
                

                } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                // 判断,并解析
                catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            };
        }.start();

    }

    // public static ArrayList<Bean> getBeanl(String json){
    //
    // return gson.fromJson(json, new TypeToken<List<Bean>>(){}.getType());
    // }

    
    class Myadapter extends BaseAdapter{

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

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(int position, View v, ViewGroup parent) {
            ViewHolder holder;
            if (v == null) {
                holder = new ViewHolder();
                v = LayoutInflater.from(MainActivity.this).inflate(R.layout.list_item, null);
                holder.content = (TextView) v.findViewById(R.id.content);
                holder.img = (SmartImageView) v.findViewById(R.id.img);
                holder.title = (TextView) v.findViewById(R.id.title);
                v.setTag(holder);
            } else {
                holder = (ViewHolder) v.getTag();
            }
            holder.img.setImageUrl(bean.get(position).getPic());
            holder.title.setText(bean.get(position).getTitle());
            holder.content.setText(bean.get(position).getIntro());
            return v;
        }
        
        
    }
    
    class ViewHolder {
        private SmartImageView img;
        private TextView title;
        private TextView content;
    }
}

---------------------------------------------------------------




package com.example.text;

public class Bean {
    private String intro;

    private String pic;

    private String title;

    public void setIntro(String intro) {
        this.intro = intro;
    }

    public String getIntro() {
        return this.intro;
    }

    public Bean(String intro, String pic, String title) {
        super();
        this.intro = intro;
        this.pic = pic;
        this.title = title;
    }

    public void setPic(String pic) {
        this.pic = pic;
    }

    public String getPic() {
        return this.pic;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getTitle() {
        return this.title;
    }
}

-----------------------------------------------------------------

xml文件


<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true" >

    </ListView>

</RelativeLayout>
---------------------------------------------------------------

<?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" >

    <com.loopj.android.image.SmartImageView
        android:id="@+id/img"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_weight="1"
        android:src="@drawable/ic_launcher" >
    </com.loopj.android.image.SmartImageView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:singleLine="true"
            android:text="英国英国英国英国英国"
            android:textColor="#f00"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:singleLine="true"
            android:text="英国英国英国英国英国"
            android:textSize="18sp" />
    </LinearLayout>

</LinearLayout>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值