Android Async doinbackaground数据到onpostexecute

时间:2014-01-25 00:46:00

标签: android android-asynctask

我需要一些帮助,从doinbackground到onpostexecute的结果。结果采用JSONArray的形式。我想通过onpostexecute在UI中填充两个textview。代码中没有错误但没有任何反应,因为看起来inpostexecute没有被调用。请帮忙。我在这里看过几个类似的问题并试了很多东西,但无法让它工作......提前谢谢。请注意私有静态最终字符串TAG_LATEST_SCORES =“cricket_scores”;

    class PostScores extends AsyncTask<JSONArray, Void, JSONArray> {

    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(NewScoreupdatescricketActivity.this);
        pDialog.setMessage("Posting LiveScore Updates..");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    /**
     * Creating product
     * */
    protected JSONArray doInBackground(JSONArray... args) {

        String runs1 = runs.getText().toString();
        String wicket1 = wicket.getText().toString();
        String overs1 = over.getText().toString();
        String rr1 = rr.getText().toString();
        String team_name = null;
        String bat1;
        if (innings.isChecked() == false) {
            bat1 = "1";
            team_name = bat.getText().toString();

        } else {
            bat1 = "2";
            team_name = bat.getText().toString();
        }
        if (rr1.equals("NA")) {
            rr1 = "0";
        }

        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("runs", runs1));
        params.add(new BasicNameValuePair("wickets", wicket1));
        params.add(new BasicNameValuePair("id", id1));
        params.add(new BasicNameValuePair("overs", overs1));
        params.add(new BasicNameValuePair("rr", rr1));
        params.add(new BasicNameValuePair("bat", bat1));
        params.add(new BasicNameValuePair("team_name", team_name));

        // getting JSON Object
        // Note that create product url accepts POST method
        JSONObject json = jsonParser.makeHttpRequest(url_create_product,
                "POST", params);

        // check log cat fro response
        Log.d("Create Response", json.toString());

        // check for success tag
        try {
            int success = json.getInt(TAG_SUCCESS);
            pDialog.dismiss();

            if (success == 1) {

                latest_scores = json.getJSONArray(TAG_LATEST_SCORES);


            } else {

            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
        pDialog.dismiss();
        return latest_scores;

    }
}

/**
 * After completing background task Dismiss the progress dialog
 * **/
protected void onPostExecute(JSONArray latest_sc) {
    // dismiss the dialog once done

    try {

        for (int i = 0; i < latest_sc.length(); i++) {

            JSONObject c = latest_sc.getJSONObject(i);

            String wickets = c.getString(TAG_WICKETS);
            String runs = c.getString(TAG_RUNS);
            score_inngs1.setText(wickets + runs);

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

}

1 个答案:

答案 0 :(得分:0)

检查括号。在定义doInBackground之后,你有一个额外的括号 - 一个关闭你的PostScores类,所以你的onPostExecute方法被定义为包含PostScores的任何类的成员。如果有的话。

此外,您需要@Override AsyncTask方法来提供自己的实现。