AsyncTask不适用于4.0.4

我创建了一个下面的类去上网,并发送一个请求到PHP脚本。 我创建它像AsyncTask而不是在主线程中,以便在4.0.4上工作,但是当我测试它时,它不起作用,虽然它在2.2上可以正常工作。 你知道什么是问题吗?

class download extends AsyncTask<String, Integer, String> {

        protected String doInBackground(String s1, String s2) {
            String result = "";
            //http post

            ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("Vreme", s1));
            nameValuePairs.add(new BasicNameValuePair("Datum", s2));

            InputStream is = null;
            try {

                String adresa = "http://senzori.open.telekom.rs/script.php";
                HttpPost httppost = new HttpPost(adresa);
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpClient httpclient = new DefaultHttpClient();

                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                is = entity.getContent();
            } catch (Exception e) {
                Log.e("log_tag", "Error in http connection " + e.toString());
            }
            //convert response to string
            try {
                BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "n");
                }
                is.close();

                result = sb.toString();

            } catch (Exception e) {
                Log.e("log_tag", "Error converting result " + e.toString());
            }
            return result;
        }

        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub
            return null;
        }
    }

这可能不起作用,因为你重载了doInBackground,但没有调用重载方法。

改变它,所以原来的方法是这样的:

@Override
protected String doInBackground(String... params) {

    return doInBackground (params[0], params[1]);
}

请注意,它现在使得重载无用,将代码移回到重载doInBackground (String... params) ,并且还需要确保在调用execute()时提供两个字符串作为参数。

链接地址: http://www.djcxy.com/p/29493.html

上一篇: AsyncTask not working on 4.0.4

下一篇: how to show list object page in android?