Android:无法通过json解析数据

时间:2012-09-19 08:11:08

标签: android json android-listview httpclient

我无法使用json解析来自服务器的数据我正在以这种格式获取数据。

[{"school":"abc","event":"test","eventdate":"09\/18\/2012","eventtext":"Hello,this is test "}][{"school":"abc","event":"test","eventdate":"09\/18\/2012","eventtext":"Hello,this is test "},{"school":"abc","event":"new test","eventdate":"09\/20\/2012","eventtext":"Hello, test2"}]

我是json parsing的新手,这是我的代码:

import java.util.ArrayList;
import java.util.HashMap;

import org.apache.http.HttpResponse;
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.JSONObject;

import android.app.Activity;

import android.os.Bundle;
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 com.appforschools.R;
import com.appforschools.event.Myadapter;

public class event extends Activity {
ListView list;

ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
 @Override

 public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.event);
   list=(ListView)findViewById(R.id.listView1);

   try {
        HttpClient client = new DefaultHttpClient();
        HttpGet get = new HttpGet(
                "http://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
        HttpResponse rp = client.execute(get);
        String result = EntityUtils.toString(rp.getEntity());
        System.out.println("----------------------- result: " + result);

        result = "{\"root\": " + result + "}";
        JSONObject root = new JSONObject(result);
        JSONArray sessions = root.getJSONArray("root");
        for(int i=0;i<sessions.length();i++){
            HashMap<String, String> map2 = new HashMap<String, String>();
            JSONObject e = sessions.getJSONObject(i);
            map2.put("text", e.getString("eventtext"));
            map2.put("date", e.getString("eventdate"));
            mylist.add(map2);
        }




    } catch (Exception e) {
        e.printStackTrace();
    }
    System.out.println("----------------------- 1");
list.setAdapter(new Myadapter());

 }
public class Myadapter extends BaseAdapter {

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

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

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

        @Override
        public View getView(final int position, View convertView,
                ViewGroup parent) {
            // TODO Auto-generated method stub
            LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.customlistview,
                    null);
            TextView item = (TextView) convertView
                    .findViewById(R.id.text);
            item.setText(mylist.get(position).get(
                    "text"));
            TextView date = (TextView) convertView
                    .findViewById(R.id.date);
            date.setText(mylist.get(position).get("date"));
            return convertView;
        }


    }


 }

当我调试此代码时,它会在此字符串String result = EntityUtils.toString(rp.getEntity());中获取结果,但在行JSONObject root = new JSONObject(result);之后显示异常。 请帮帮我这个

1 个答案:

答案 0 :(得分:0)

因为响应是一个json数组,你应该这样做:

JSONArray root = new JSONArray(result);
for(int i=0;i<root.length();i++){
....
}