我正在尝试从静态函数执行doInBackground AsycTask,但是当我尝试输入Void的parms时,我得到一个Java错误,说'Void'不适用于任务 'String,Void,Boolean'
无论如何,这是我的代码:
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 "";
}
}
}
答案 0 :(得分:1)
new RequestHandler(context).execute(URL);
protected Boolean doInBackground(String... params) {
....} 答案 1 :(得分:0)
如果你在doLogin方法中调用RequestHandler,你试图使用Void作为对象,但它是一个类型,尝试将该行更改为:
new RequestHandler(context).execute(URL, null, false);
并让你的AsyncTask返回null。