我在下面的代码中收到以下错误:返回类型与AsyncTask.onPostExecute(Integer)不兼容。我试图从doInBackground任务中完成的http请求返回结果。我得到错误:类型不匹配:无法在isAvailable的返回语句中从AsyncTask转换为int。我觉得有些事情很简单,我没有做,但我无法弄清楚。Android AsyncTask(从doInBackground返回一个整数)
public int isAvailable(int position) {
GetIsAvailable isAvail = new GetIsAvailable();
Integer nisAvail = isAvail.execute(position); // error is still here
return nisAvail;
}
private class GetIsAvailable extends AsyncTask<Integer,Void,Integer > {
@Override
protected Integer doInBackground(Integer...position) {
Bundle resBundle = new Bundle();
String url = "http://www.testurl.com"
+ position[0]+"&uname="+AppStatus.mUserName;
URL iuri;
try {
iuri = new URL(url);
URLConnection connection = iuri.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestProperty("Content-type",
"application/x-www-form-urlencoded");
BufferedReader br = new BufferedReader(new InputStreamReader(
(InputStream) connection.getContent()));
resBundle.putInt("isAvail", Integer.parseInt(br.readLine().trim()));
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return new Integer(0);
}
@Override
protected Integer onPostExecute(Integer isAvail) { // Main Error here
return isAvail;
}
这可能是完全关闭基地(?哎,我在课堂上,现在,我得到了一通,右),但你不应该'返回新的整数(0)' ? – kcoppock
是的,但似乎我仍然有相同的错误。 – user836200