2 u010033310 u010033310 于 2014.09.13 00:58 提问

请问,android客户端怎样解析服务器返回的数据并用adapter填充list呢?
请大家帮忙看看错误在哪,我也不知道我解析对不对,线程也没摸到门。

服务器返回的Json是:

{

"a":["一"."二"],

"b":{"1":"1","2":"2"},

"c":{"1":"3","2":"4"},

"d":{"1":"5","2":"6"},​​

"e":{"1":"7","2":"8"}

}

客户端代码(没有放上导入包):

public class MainActivity extends ActionBarActivity {

Context myContext;
//public Thread myThread;
EditText username;
EditText userpwd;
ListView myListView;
Button ok;
String uname1;
String pwd1;
String resultString;
private SimpleAdapter simpleAdapter=null;
List<Map<String, Object>> listItems = null;
private  Handler myHandler;

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

    username=(EditText)findViewById(R.id.uname);
    userpwd=(EditText)findViewById(R.id.pwd);
    myListView=(ListView)findViewById(R.id.item);
    ok=(Button)findViewById(R.id.submit);
    myHandler = new Handler()
    {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0x123) {

                simpleAdapter=new SimpleAdapter(myContext, listItems, R.layout.carditem, 
                        new String[]{"cj","xf","xs","km","zt"}, 
                        new int[]{R.id.chengji,R.id.xuefen,R.id.xueshi,R.id.kecheng,R.id.zhuangtai});

                myListView.setAdapter(simpleAdapter);

            }
        }
    };


    ok.setOnClickListener(new View.OnClickListener()
    {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            new mythread().start();
        }

    });
}
private class mythread extends Thread
{
    public void run()
    {

        uname1=username.getText().toString();
        pwd1=userpwd.getText().toString();
        //contTent.setText(uname1+pwd1);

        String url="http://202.115.137.32:8379/demo1/login.php";
        HttpPost myPost=new HttpPost(url);

        List<NameValuePair>parms=new ArrayList<NameValuePair>();
        parms.add(new BasicNameValuePair("uname", uname1));
        parms.add(new BasicNameValuePair("upwd", pwd1));

        try 
        {
            myPost.setEntity(new UrlEncodedFormEntity(parms));
        } 
        catch (UnsupportedEncodingException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        HttpClient myhttp=new DefaultHttpClient();
            HttpResponse response=null;
            try {
                response = myhttp.execute(myPost);
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            try {
                resultString = EntityUtils.toString(response.getEntity(),"UTF-8");
            } catch (ParseException | IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }  
            try {
                JSONObject jsonObject=new JSONObject(resultString);
                JSONArray km=jsonObject.getJSONArray("a");
                JSONObject cj=jsonObject.getJSONObject("b");
                JSONObject xs=jsonObject.getJSONObject("c");
                JSONObject xf=jsonObject.getJSONObject("d");
                JSONObject zt=jsonObject.getJSONObject("e");
                listItems=new ArrayList<Map<String,Object>>();
                for (int i = 0; i < km.length(); i++) 
                {

                    Map<String, Object> listItem=new HashMap<String,Object>();
                        listItem.put("km", km.getString(i));
                        listItem.put("cj", cj.optString(i+"1","无"));
                        listItem.put("xs", xs.optString(i+"1","无"));
                        listItem.put("xf", xf.optString(i+"1","无"));
                        listItem.put("zt", zt.optString(i+"1","无"));
                    listItems.add(listItem);
                }

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


        myHandler.sendEmptyMessage(0x123);
    }
}

}

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!