gpt4 book ai didi

android - AsyncTask 不适用于 4.0.4

转载 作者:太空狗 更新时间:2023-10-29 16:06:09 25 4
gpt4 key购买 nike

我在下面创建了一个类,它连接到互联网并向 php 脚本发送请求。为了在 4.0.4 上工作,我像 AsyncTask 一样创建了它,而不是在主线程中创建它,但是当我测试它时,它不起作用,尽管它在 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 时(),您提供两个字符串作为参数。

关于android - AsyncTask 不适用于 4.0.4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14287656/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com