CURRENT SCENARIO
我要求我必须在点击按钮时运行asynctask
并将一些数据发送到服务器,并且不向用户显示任何进度。现在,如果用户再次按下该按钮,将执行新的asynctask实例,而无需等待以前的版本完成。这个我可以使用这个类。
public class AsyncTaskTools {
public static <P, T extends AsyncTask<P, ?, ?>> void execute(T task) {
execute(task, (P[]) null);
}
@SuppressLint("NewApi")
public static <P, T extends AsyncTask<P, ?, ?>> void execute(T task, P... params) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params);
} else {
task.execute(params);
}
}
}
问题
现在,当我在Android Kitkat中运行相同的代码时,它不会在第一次asynctask之后给出成功响应,而且只有当我点击它们之间的间隔很短的按钮时才会这样做。如果我在再次点击按钮之前等待5-10秒,那么我的代码工作得很好,但当我快速点击按钮约5次时,我的代码无法正常工作。
以下
只能解决与此相关的问题 exceeded content-length limit of 12900 bytes
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
此异常在catch块中被捕获一段时间。
这是我完整的asynctask代码。
class SendCallDispositionAsyncTask extends AsyncTask<Void, Void, Void> {
public SendCallDispositionAsyncTask() {
// TODO Auto-generated constructor stub
//this.activity = profile_Info;
}
protected void onPreExecute() {
super.onPreExecute();
// utils.showProgressDialog(ActivityCallScreen.this, "Loading. Please wait..");
}
@SuppressWarnings("deprecation")
@Override
protected Void doInBackground(Void... params) {
String responseArray = null;
Log.d("asyncresponse", "do in background");
CallDurationReceiver.start_time=0;
try {
byte[] data;
try {
Log.d("callduration","Duration: "+prefManager.getDouble(PrefrenceConstants.CALL_DURATION));
MultipartEntityBuilder entity = MultipartEntityBuilder.create();
entity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart("userid", new StringBody(String.valueOf(prefManager.getInt(PrefrenceConstants.USER_ID))));
entity.addPart("dialerno", new StringBody(mobile));
entity.addPart("dipositionid", new StringBody(dispositionId));
entity.addPart("dipositiongroup", new StringBody(dipositiongroup));
entity.addPart("callduration", new StringBody(prefManager.getDouble(PrefrenceConstants.CALL_DURATION)+""));
entity.addPart("callename", new StringBody(name));
entity.addPart("calleage", new StringBody(age));
entity.addPart("callecontact", new StringBody(contact));
entity.addPart("isnote", new StringBody(checked));
entity.addPart("cbdate", new StringBody(date));
entity.addPart("aliment", new StringBody(aliment));
entity.addPart("callelocation", new StringBody(location));
entity.addPart("remarks", new StringBody(selectedRemarks));
entity.addPart("file", new FileBody(file));
entity.addPart("contactid", new StringBody(String.valueOf(contact_id)));
HttpEntity httpEntity = entity.build();
URL url = new URL(sendDispositionUrl);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setReadTimeout(60000);
httpURLConnection.setConnectTimeout(60000);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setUseCaches(false);
httpURLConnection.setDoOutput(true);
httpURLConnection.setRequestProperty("Authorization", prefManager.getString(PrefrenceConstants.API_KEY));
httpURLConnection.setRequestProperty("Connection", "Keep-Alive");
// httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpURLConnection.addRequestProperty("Content-length", httpEntity.getContentLength() + "");
httpURLConnection.addRequestProperty(httpEntity.getContentType().getName(), httpEntity.getContentType().getValue());
OutputStream os = httpURLConnection.getOutputStream();
httpEntity.writeTo(httpURLConnection.getOutputStream());
os.close();
httpURLConnection.connect();
if (httpURLConnection.getResponseCode() == HttpsURLConnection.HTTP_OK) {
response = BufferReaderMaker.readContentFromIS(httpURLConnection.getInputStream());
Log.v("log_tag", "image upload status ::: " + response);
} else if (httpURLConnection.getResponseCode() == HttpsURLConnection.HTTP_CREATED) {
response = BufferReaderMaker.readContentFromIS(httpURLConnection.getInputStream());
Log.d("responce", response);
}
// {"error":true,"message":"Required field(s) userid, dialerno, dipositionid, dipositiongroup, callduration, contactid is missing or empty"}
} catch (final UnknownHostException e) {
// TODO: handle exception
Log.d("host", e.getMessage());
}
} catch (final ConnectTimeoutException e) {
// TODO: handle exception
Log.d("timeout", e.getMessage());
} catch (Exception e) {
e.printStackTrace();
Log.d("exec", e.getMessage());
}
return null;
// return "Success";
}
protected void onPostExecute(Void result) {
super.onPostExecute(result);
Log.d("asyncresponse", response);
// utils.hideProgressDialog();
}
}
我查了几个关于这个问题的链接,但没有一个对我有用。
有人可以告诉我这是否是我的代码或服务器端问题。
修改
此外,我正在将文件上传到目前大约50 kb的服务器。这会导致任何问题吗?
答案 0 :(得分:1)
听起来您的问题可能是您发布的内容的长度。
postcss file.css --replace --use autoprefixer
您需要以字节为单位获取长度。您应该检查httpURLConnection.addRequestProperty("Content-length", httpEntity.getContentLength() + "");
返回的值。它应该是您上传的字节数 - 包括文件的大小。
您可能对可上传的数据量有一些限制 - 但我怀疑这是问题,因为错误消息中的字节数不多。
我使用此代码没有问题:
httpEntity.getContentLength()
我以不同的方式添加数据,但您可以调整解决方案。