我在下面的代碼中收到以下錯誤:返回類型與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