从PHP脚本循环返回的JSON数组不正确

时间:2012-03-29 00:23:25

标签: php android json

我一直在为一个更大的项目编写一些代码来填充Android中的listview。长话短说,我只能让模拟器循环遍历JsonArray中的第一项,即使我知道从我的PHP脚本返回的结果包含3组数据。

这是我的代码:

public void getChosen(String username) {
    String chosenFriend,msg,type,holder;
    Bitmap profilepic;
        try {
            // Creating HTTP Post
            HttpPost httppost = new HttpPost(
            "mysite/chosenlist.php");
            // Builds post parameters key and value pair
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("username", username));
            // Url Encoding the POST parameters
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            // Making HTTP Request
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();

        }

        catch (Exception e) {
            // writing error to Log
            e.printStackTrace();
            //return null;

        }
        try {

            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            sb = new StringBuilder();
            sb.append(reader.readLine() + "\n");
            String line = "0";

            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }

            is.close();
            result = sb.toString();
            System.out.println(result);

        } catch (Exception e) {
            // writing error to Log
            e.printStackTrace();
            //return null;

        }
        try {
            jArray = new JSONArray(result);
            JSONObject json_data=null;
            int jAL = jArray.length();

            for(int i=0;i<jAL;i++){
                 json_data = jArray.getJSONObject(i);


                System.out.println(json_data);
                 chosenrow cr = new chosenrow();
                 holder = json_data.getString("Sender");


                 if(json_data.getString("Type").contentEquals("1")){
                 if (holder.equalsIgnoreCase(username)){
                     cr.setUser(json_data.getString("Reciever")); 
                     chosenFriend = json_data.getString("Reciever");
                     System.out.println(chosenFriend);
                 }
                 else{
                     cr.setUser(json_data.getString("Sender")); 
                     chosenFriend = json_data.getString("Sender");
                 }
                 type = json_data.getString("Type");
                 cr.setStatus(type);

                 msg = getLastMsg(chosenFriend);
                 cr.setLastMsg(msg);
                 String time = getLastMsgTime(chosenFriend);
                 cr.setTime(time);
                 profilepic = getPicture(chosenFriend);
                 cr.setPic(profilepic);
                 chosen.add(cr);             
                System.out.println(cr);
                 }
                 else{
                     if (holder.equalsIgnoreCase(username)){
                         cr.setUser(json_data.getString("Reciever")); 
                         chosenFriend = json_data.getString("Reciever");
                         System.out.println(chosenFriend);
                     }
                     else{
                         cr.setUser(json_data.getString("Sender")); 
                         chosenFriend = json_data.getString("Sender");
                     }
                     type = json_data.getString("Type");
                     cr.setStatus(type);

                     msg = "Pending";
                     cr.setLastMsg(msg);
                     String time = "Awaiting Request Response";
                     cr.setTime(time);
                     profilepic = getPicture(chosenFriend);
                     cr.setPic(profilepic);
                     chosen.add(cr);             


                 }
            }

        } catch (Exception e) {
            // writing error to Log
            e.printStackTrace();
            //return null;

        }




    }

为什么我不能循环到数组中的第一项之外的任何线索?

感谢。

0 个答案:

没有答案