我有一个从我的服务器获取文件的异步任务。它需要很长时间才能开始,但是当它开始时它很快。在移动设备上它在大约5秒内完成,而在wifi上我可以等待40秒+。
这是asynctask: 公共类ProgressTask扩展AsyncTask { @override
protected Void doInBackground(String... params) {
// in background
for (int i = 0; i < selectedWidgets.size(); i++) {
saveWidget(selectedWidgets.get(i));
}
}
return null;
}
}
所以它启动savewidget。 savewidget =下载文件并将其存储在本地
这是savewidget:
public void saveWidget(int widgetnumber) {
try {
// set location and name
File root = new File(Environment.getExternalStorageDirectory(),
"widgets");
jsonPath = root.getAbsolutePath() + "/widget";
File gpxfile = new File(root, "widget" + widgetnumber + ".txt");
if (!gpxfile.exists()) {
// if file doesn't excists, get it from the internet
HttpParams httpParameters = new BasicHttpParams();
// add extra time to get it before error is thrown
HttpConnectionParams.setConnectionTimeout(httpParameters, 0);
HttpConnectionParams.setSoTimeout(httpParameters, 0);
HttpConnectionParams.setTcpNoDelay(httpParameters, true);
DefaultHttpClient httpclient = new DefaultHttpClient(
httpParameters);
// set everyting to get it from the internet
link = "http://somestuff/widget_id="
+ widgetnumber;
HttpPost httppost = new HttpPost(link);
InputStream inputStream = null;
String result = null;
BufferedReader reader = null;
// set response
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
inputStream = entity.getContent();
// read and save it
reader = new BufferedReader(new InputStreamReader(inputStream,
"UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e2) {
e2.printStackTrace();
}
result = sb.toString();
// code is in app
if (!root.exists()) {
root.mkdirs();
}
// write it to new file
FileWriter writer = new FileWriter(gpxfile);
writer.append(result);
writer.flush();
writer.close();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
任何能够解释为什么需要这么多时间来获取一个小的无线网络以及它在移动互联网上快速发展的人我有点无能为力。
这是一个好方法还是更好?
也许不错,在某些时候我使用服务中的多部分来向服务器发送视频和数据。它有效,但就像这样它很慢。一旦第一个视频上传,它就会很快。
发送视频的多部分实体:
public Void SendToServer(String aUrl, String file, String parameters,
String uuid, String uploaded, int id) {
// set extra settings for upload
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, 0);
HttpConnectionParams.setSoTimeout(params, 0);
HttpConnectionParams.setTcpNoDelay(params, true);
params.setParameter(CoreProtocolPNames.WAIT_FOR_CONTINUE,
HttpVersion.HTTP_1_1);
// add thes settings
HttpClient httpClient = new DefaultHttpClient(params);
// set uploadlocation
HttpPost httpPost = new HttpPost(aUrl);
try {
jsonParameters = new JSONObject(parameters);
} catch (JSONException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
stopSelf();
}
// set video
FileBody fb = new FileBody(new File(file));
// get extra parameters from json and add them to entity
try {
MultipartEntity entity = new MultipartEntity(
HttpMultipartMode.STRICT);
Iterator<String> iter = jsonParameters.keys();
// loop as long as there are keys
while (iter.hasNext()) {
String key = iter.next();
try {
value = jsonParameters.get(key);
} catch (JSONException e) {
// Something went wrong!
stopSelf();
}
// add them
entity.addPart(key, new StringBody(value.toString()));
}
// continue to add data to entity
entity.addPart("widget_uuid", new StringBody(uuid));
entity.addPart("widget_uploaded", new StringBody(uploaded));
entity.addPart("file", fb);
httpPost.setEntity(entity);
// get response from server
HttpResponse response = httpClient.execute(httpPost);
try {
// get statuscode from server
HttpEntity resEntity = response.getEntity();
String serverRespons = "";
if (serverRespons != null) {
serverRespons = EntityUtils.toString(resEntity);
}
serverRespons = serverRespons.substring(
serverRespons.indexOf("(") + 1,
serverRespons.indexOf(")"));
try {
jsonServerRespons = new JSONObject(serverRespons);
} catch (JSONException e) {
}
try {
statusServerRespons = jsonServerRespons.getString("status");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.i("test", statusServerRespons);
// delete successfully sended video's
if (statusServerRespons.equalsIgnoreCase("ok")) {
vds.deleteTitle(id);
}
} catch (ClientProtocolException e1) {
// Something went wrong!
stopSelf();
} catch (IOException e1) {
// Something went wrong!
stopSelf();
}
return null;
} catch (IOException e) {
// Something went wrong!
stopSelf();
}
return null;
}
提前致谢