尝试使用
异步任务
进行HTTP post时,会出现以下错误:ASyncTask: DoInBackground(String...) clashes with DoInBackground(Params...) in Android.os.AsyncTask; attempting to use incompatible return type
我该如何修复此问题?这是我第一次使用AsyncTask。
导致错误的具体代码行:
@Override
protected String doInBackground(String... params) {
完整AsyncTask的代码:
private class MyTask extends AsyncTask<String, Void, Void>
{
boolean success = false;
@Override
protected String doInBackground(String... params) {
StringBuilder respData = new StringBuilder();
URL url = new URL("MY_URL");
URLConnection conn = url.openConnection();
HttpURLConnection httpUrlConnection = (HttpURLConnection) conn;
httpUrlConnection.setUseCaches(false);
httpUrlConnection.setRequestProperty("User-Agent", "App");
httpUrlConnection.setConnectTimeout(30000);
httpUrlConnection.setReadTimeout(30000);
httpUrlConnection.setRequestMethod("POST");
httpUrlConnection.setDoOutput(true);
OutputStream os = httpUrlConnection.getOutputStream();
//InputStream postStream = toInputStream(toSubmit, "UTF-8");
InputStream stream = new ByteArrayInputStream(toSubmit.getBytes(StandardCharsets.UTF_8));
try {
copy(stream, os);
} finally {
stream.close();
os.flush();
os.close();
}
httpUrlConnection.connect();
int responseCode = httpUrlConnection.getResponseCode();
if (200 == responseCode) {
InputStream is = httpUrlConnection.getInputStream();
InputStreamReader isr = null;
try {
isr = new InputStreamReader(is);
char[] buffer = new char[1024];
int len;
while ((len = isr.read(buffer)) != -1) {
respData.append(buffer, 0, len);
}
} finally {
if (isr != null)
{
isr.close();
success = true;
}
}
is.close();
}
else {
// use below to get error stream
// inputStream = httpUrlConnection.getErrorStream();
}
return "done";
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
Toast toast = Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_SHORT);
}
}