0
我試圖從一個靜態函數執行doInBackground AsycTask,但是當我嘗試輸入Void的parms時,出現Java錯誤,說'Void'不是適用於任務 '字符串,太虛,布爾'在靜態函數中執行AsyncTask的問題
總之,這裏是我的代碼:
public class RequestHandler extends AsyncTask<URL, Void, Boolean> {
//atributes for Async
private static Context context;
private RequestHandler origin;
// Constructor
public RequestHandler(Context c) {
origin = this;
context = c;
}
public static boolean doLogin(String username, String password) {
String URL = "http://127.0.0.1:1337";
new RequestHandler(context).execute(URL, Void, false); // what do I put here?
}
@Override
protected Boolean doInBackground(URL... params) {
// TODO Auto-generated method stub
URL url = null;
try {
HttpURLConnection urlConnection = null;
try {
urlConnection = (HttpURLConnection) url.openConnection();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
InputStream in = null;
try {
in = new BufferedInputStream(urlConnection.getInputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
readStream(in);
} finally {
}
return false;
}
private String readStream(InputStream is) {
try {
ByteArrayOutputStream bo = new ByteArrayOutputStream();
int i = is.read();
while(i != -1) {
bo.write(i);
i = is.read();
}
return bo.toString();
} catch (IOException e) {
return "";
}
}
}
什麼!?把DoLogin放在課堂外面? !?? – TheBlueCat 2012-07-24 14:51:22
是的,爲什麼你把我鏈接到一個Async教程?我知道所有變量傳遞到哪裏。 – TheBlueCat 2012-07-24 14:52:39
嗯,如果你知道它非常好,那麼不需要問這裏... – 2012-07-24 15:05:18