如何从AsyncTask获得价值?

时间:2014-04-18 11:16:48

标签: android android-asynctask

首先,请查看下面的代码。

我的AsyncTask代码:

public class loadNewMeeting extends AsyncTask<String, String, String> {

    int intCount = 0;
    JSONObject jnewmeeting;
    JSONArray attendants;

    public String doInBackground(String... args) {          
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("attendant[userid]", Userid));

        jnewmeeting = jParser.makeHttpRequest(str_url_att, "POST", params);

        try {
            int success = jnewmeeting.getInt("msg");
            if (success == 1) {
                attendants = jnewmeeting.getJSONArray("attendant");
                for (int i = 0; i < attendants.length(); i++) {
                JSONObject c = attendants.getJSONObject(i);
                    int intRole = c.getInt("role");
                    if (intRole == 3 ){
                        intCount = intCount +1;
                    }                       
                }   
            } 
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return String.valueOf(intCount);
    }

    protected void onPostExecute(String file_url) {
        countMeeting = String.valueOf(intCount);    
        SetCount(countMeeting);
            if (intMeetingNum < intCount)
                Toast.makeText(getBaseContext(), "You get " + (intCount - intMeetingNum) + " new meeting(s)", Toast.LENGTH_LONG).show();
            intMeetingNum = intCount;
    }
}

我无法从AsyncTask外部获取countMeeting。我尝试制作Set and Get方法,但countMeeting仍然为null。寻求帮助。

感谢。

1 个答案:

答案 0 :(得分:0)

为什么不从“doInBackground”方法返回countMeeting?

由于你没有使用“file_url”返回值,在“onPostExecute”方法中,只需更改“doInBackground”方法的返回值,“doInBackground”方法的签名,“onPostxecute”的签名“和AsyncTask初始化的第三个参数。