AsyncTask继续返回null

时间:2015-11-19 04:57:54

标签: android android-fragments android-asynctask

我使用for循环来调用asynctask 这就是我在片段中调用asynctask的方式:

private void LoadPartiList() {
for (int i = 0; i < participants.length; i ++){
    String getlistparti = cmd.getPartList();
    participants = getlistparti.split(",");
    partiparti = ((CommentandLikeActivity)getActivity()).getparticipantlist(participants[i]);
    Log.d("test","testlogcat " + partiparti);

}

这里是我的asynctask,它在包含片段的活动中分配:

public ArrayList<User> getparticipantlist(final String participationID) {
    final ArrayList<User> list = new ArrayList<User>();
    final User user = new User();
    new AsyncTask<String, String, ArrayList<User>>() {

        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected ArrayList<User> doInBackground(String... params) {


            // updating UI from Background Thread
            runOnUiThread(new Runnable() {
                public void run() {
                    // Check for success tag
                    int success;
                    try {
                        // Building Parameters

                        Log.d("participantid", "17112015 " + participationID);
                        List<NameValuePair> params = new ArrayList<NameValuePair>();
                        params.add(new BasicNameValuePair("userid", participationID));


                        // getting product details by making HTTP request
                        // Note that product details url will use GET request
                        JSONObject json = jsonParser.makeHttpRequest(
                                "http://192.168.168.111:80/testing/participationlist.php", "GET", params);

                        // check your log for json response
                        Log.d("Single Product Details", json.toString());

                        // json success tag
                        success = json.getInt("success");
                        if (success == 1) {
                            // successfully received product details
                            JSONArray productObj = json
                                    .getJSONArray("product"); // JSON Array

                            // get first product object from JSON Array
                            JSONObject product = productObj.getJSONObject(0);





                            user.setId(String.valueOf(product.getString(String.valueOf("vuid"))));
                            user.setUsername(product.getString("vusername"));
                            user.setProfileimage(product.getString("vprofileimage"));
                            list.add(user);

                            Log.d("","getwalalala " +product.getString(String.valueOf("vuid")) +" : "+ product.getString("vusername") + " : " +product.getString("vprofileimage") );


                        }else{
                            // product with pid not found
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });

            return list;
        }



        @Override
        protected void onPostExecute(ArrayList<User> list) {
            List<User> mUserlist = new ArrayList<User>();
            mAdapterr = new participantlistAdapter(getApplicationContext(), mUserlist);
            mAdapterr.Update();
            mAdapterr.add(user);

        }
    }.execute(null, participationID, null);

    return list;
}

这是我在运行Asynctask

时记录的内容

Logcat for Log.d("","getwalalalala")

但为什么它只会返回[]或null?我的代码有什么问题吗?请帮忙... 我想根据participant.length中的长度循环userid以从phpserver获取用户名并在listview中显示,我该怎么做? 在线研究了几天后我没有得到答案

1 个答案:

答案 0 :(得分:0)

当您尝试从AsyncTask返回数据时,这是不正确的。 AsyncTask doInBackground将数据返回给onPostExecute。因此,无论您想要返回什么,都只能在onPostExecute中接收。但是你仍然希望实现Asynctask,然后通过以下方式之一实现:

  1. 通过将AsyncTask定义为活动中的内部私有类,并直接使用onPostExecute中的数据来更新您的ui。

  2. 第二种方法是将接口回调传递给你的asynctask,在该接口的帮助下,回调将你的ui从AsyncTask更新为活动。