如何在不使用RoboSpice等库的情况下实现长时间运行的网络操作(如上传一大堆照片)的原生Android方式是什么?如何在不使用AsyncTask而不使用库的情况下实现长时间运行的网络上传
我已经阅读了很多关于stackoverflow的话题,表明asynctask不适合长时间运行的操作,因为它与活动的生命周期密切相关,可能导致内存泄漏,并且由于android 3.2只有一个线程适用于所有的asynctasks为一个应用程序。 (不知道这最后一个)
我如何取代我的asynctask与别的东西?
现在,我听说过处理程序,执行程序,服务以及哪些不是,但是我如何在代码中实现它们以及选择哪一个?
下面是我用
我已经删除了大量的代码的AsyncTask的例子,只是让你可以看到基本结构
public class UploadPhotosToServer extends AsyncTask<String, Void, Boolean> {
@Override
protected Boolean doInBackground(String... args) {
HashMap<String, String> params = new HashMap<String, String>();
try {
if(uploadImageToServer(id, path, params)) {
success = true;
} else {
success = false;
}
} catch (Exception e) {
e.printStackTrace();
success = false;
}
return success;
}
public boolean uploadImageToServer(int imageId, String imagePath, HashMap<String, String> params) throws Exception {
try {
JSONObject json = jsonParser.uploadImageToServer(imagePath, params);
JSONObject message = json.getJSONObject("message");
String serverResponse = message.getString("success");
if (serverResponse.contentEquals("true") {
return true;
} else {
return false;
}
} catch (JSONException e) {
e.printStackTrace();
return false;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}
,这里是jsonParser.uploadImageToServer
public JSONObject uploadImageToServer(String imagePath, HashMap<String, String> params) throws Exception {
HttpResponse response;
MultipartEntityBuilder multipartEntity;
HttpPost postRequest;
HttpContext localContext;
Bitmap bitmap;
try {
// Set the http handlers
httpClient = new DefaultHttpClient();
localContext = new BasicHttpContext();
postRequest = new HttpPost(SERVER + "images");
// Send the package
multipartEntity = MultipartEntityBuilder.create();
multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
multipartEntity.addPart("file", new FileBody(new File(imagePath)));
for (Map.Entry<String, String> entry : params.entrySet()) {
multipartEntity.addTextBody(entry.getKey(), entry.getValue());
}
postRequest.setEntity(multipartEntity.build());
// Get the response. we will deal with it in onPostExecute.
response = httpClient.execute(postRequest, localContext);
HttpEntity httpEntity = response.getEntity();
inputStream = httpEntity.getContent();
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
json = sb.toString();
inputStream.close();
reader.close();
} catch (ClientProtocolException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
// Try parsing the string to a JSON object
try {
jsonObject = new JSONObject(json);
} catch (JSONException e) {
e.printStackTrace();
}
// Return JSON String
return jsonObject;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
AsyncTask有什么问题?它使线程的使用变得容易。 – Simas 2014-08-31 09:16:29
我建议写一个'报警管理器'调用'服务'上传数据在后台线程中使用asynctask – 2014-08-31 09:23:25
我已经阅读了很多关于stackoverflow的话题,建议asynctask不适合长时间运行的操作,因为它是密切相关的到一个活动的生命周期,可能会导致内存泄漏,并且由于android 3.2只有一个线程适用于应用程序的所有asynctasks。 – 2014-08-31 09:38:09