解析JSON并将其添加到ListView

时间:2015-03-18 00:01:41

标签: android json

我一直在为自己做这个例子很长一段时间,我希望有人可以帮助我,因为我觉得我非常接近答案!无论如何,我正在尝试解析的东西看起来像[{“fromUser”:“Andrew”},{“fromUser”:“Jimmy”}],我之前请求过帮助,但我不觉得好像我和现在一样亲密。我正在做的是解析它,然后将它添加到另一个类的列表视图中。这是我目前正在使用的课程。在调用此类之前,将发送一个帖子,即结果(上面的JSON)

class GetPendingRequests extends AsyncTask<Void, Void, Void> {

    private static final String TAG_FROMUSER = "fromUser";
    ArrayList<HashMap<String, String>> pendingUsers;
    Context friendsContext;
    ListView friendsList;
    public void setUp(ArrayList<HashMap<String, String>> pendingUsers, Context context, ListView friendsList)
    {
        this.pendingUsers = pendingUsers;
        this.friendsContext = context;
        this.friendsList = friendsList;
    }
    protected Void doInBackground(ArrayList<HashMap<String, String>> pendingUsers, JSONArray pendingRequests) {
        HTTPSendPost httpSendPost = new HTTPSendPost();
        String jsonStr = httpSendPost.doInBackground("http://www.brocksportfolio.com/GetPendingRequests.php");

        Log.d("Response: ", "> " + jsonStr);
        if (jsonStr != null) {
            try {
                JSONObject jsonObj = new JSONObject(jsonStr);
               pendingRequests = jsonObj.getJSONArray(TAG_FROMUSER);
                for (int i = 0; i < pendingRequests.length(); i++) {
                    {

                        JSONObject c = pendingRequests.getJSONObject(i);
                        String fromUser = c.getString(TAG_FROMUSER);
                        HashMap<String, String> user = new HashMap<String, String>();
                        user.put(TAG_FROMUSER, fromUser);
                        pendingUsers.add(user);
                    }
                }


            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    protected Void doInBackground(Void... params) {
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        {

            ListAdapter adapter = new SimpleAdapter(friendsContext, pendingUsers
                    , R.layout.activity_friends, new String[]{TAG_FROMUSER}, new int[]{
                    R.id.friendslistView});

            friendsList.setAdapter(adapter);

        }
    }
}

我遇到的问题是,在我尝试之前,只要它到达新页面就会崩溃。现在它做什么它只是显示一个空白活动,并没有填写列表视图。谢谢!

logcat的

03-17 20:10:46.784 19099-19099/com.skyrealm.brockyy.findmypeepsapp D/Response:﹕ > null 



public class FriendsActivity extends ActionBarActivity{

JSONArray pendingRequests = null;
ArrayList<HashMap<String, String>> pendingUsers;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_friends);
    //DECLARATION
    ListView list = (ListView) findViewById(R.id.friendslistView);
    View friendView = findViewById(R.id.friendsActivity);



   // EXAMPLE:
   // final String latitude = getIntent().getExtras().getString("latitude");
    //On touch swipe listener for swipe right method
    friendView.setOnTouchListener(new OnSwipeTouchListener(FriendsActivity.this) {
        //calls on the swipeRight method
        public void onSwipeRight() {
            Intent intent = new Intent(FriendsActivity.this, MainActivity.class);
            startActivity(intent);
        }

    });
    //end the swipe command
    //send post request
    String htmlUrl = "http://brocksportfolio.com/GetPendingRequests.php";

    HTTPSendPost postSender = new HTTPSendPost();
    postSender.Setup(500, 500, "tesT", htmlUrl);
    postSender.execute();
    //end sending post request

    //create new class object
    GetPendingRequests pendingRequest = new GetPendingRequests();
    //do in background
    pendingRequest.doInBackground(pendingUsers, pendingRequests);

   GetPendingRequests setup = new GetPendingRequests();
    setup.setUp(pendingUsers, FriendsActivity.this, list);
}

1 个答案:

答案 0 :(得分:0)

您的代码中确实存在很多不好的事情。我会给你一些提示:

1)AsyncTask应该使用方法.execute()而不是.doInBackgroud()启动。

2)您无法按照您的建议将参数传递给方法.doInBackground()。您可以指定它的位置 - &gt; AsyncTask<Void, Void, Void>

3)您已经提到Responsenull。您确定,HTTPSendPost正常工作并且真正为您获取数据吗?在我看来不是。

4)传递ListViewAsyncTask的引用是非常糟糕的模式。不要这样做。如果活动结束且AsyncTask仍在运行,您将获得NullPointerException

我的建议 - 开始here (AsyncTask docu)并仔细阅读。