I have an async task which fetches a file from my server. it takes long before it starts but when it starts it is fast. on mobile its is done in about 5 seconds while on wifi i can wait for 40seconds+.
this is the asynctask: public class ProgressTask extends AsyncTask { @Override
protected Void doInBackground(String... params) {
// in background
for (int i = 0; i < selectedWidgets.size(); i++) {
saveWidget(selectedWidgets.get(i));
}
}
return null;
}
}
so it starts savewidget. savewidget = download file and store it local
here is the 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();
}
}
anyone who can explain why it needs so much time just to fetch a small over wifi and it goes fast over mobile internet? i'm kinda clueless atm.
is this a good way or is there a better?
maybe not bad to mention, at some point i use multipartentity in a service to send a video and data to the server. it works but just like this it is slow. once the first video is uploaded it goes fast.
multipartentity to send video:
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;
}
thanks in advance