来自onProgressUpdate的Toast没有显示出来

时间:2015-08-20 18:42:53

标签: android

一切正常,但onProgressUpdate的Toast没有显示。我肯定做错了什么。请帮帮我。 守则是:

private class MyTask extends AsyncTask<String, String, String> {
    protected String doInBackground(String... params) {
        publishProgress("Sending...");
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://www.projectyourself.96.lt/android.php");
        //HttpPost httppost = new HttpPost("http://www.yembed.in/loans4you/form.php");
        try {
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
            nameValuePairs.add(new BasicNameValuePair("message", params[0]));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            httpclient.execute(httppost);
        }catch (Exception e){
            return "fail";
        }
        return "sent";
    }

    protected void onProgressUpdate(String i) {
       super.onProgressUpdate(i);
       Toast.makeText(getBaseContext(),i,Toast.LENGTH_SHORT).show();
    }

    protected void onPostExecute(String r) {
        if(r=="sent") {
            t.setText("");
            Toast.makeText(getBaseContext(), "Sent", Toast.LENGTH_SHORT).show();
        }
            else
            Toast.makeText(getBaseContext(),"Not Sent!",Toast.LENGTH_SHORT).show();
    }

}

1 个答案:

答案 0 :(得分:1)

方法onProgressUpdate(String ... values)中的参数必须是一个数组。您创建了一个从未实际调用过的全新方法。

要解决您的问题,请更改此信息:

protected void onProgressUpdate(String i) {
   super.onProgressUpdate(i);
   Toast.makeText(getBaseContext(),i,Toast.LENGTH_SHORT).show();
}

到此:

protected void onProgressUpdate(String... i) {
   super.onProgressUpdate(i);
   Toast.makeText(getBaseContext(),i[0],Toast.LENGTH_SHORT).show();
}