Improve live monitoring

This commit is contained in:
Victor Shcherb 2012-02-02 23:26:47 +01:00
parent 0a0d132d14
commit 8c25d57f74

View file

@ -1,17 +1,23 @@
package net.osmand.plus.activities;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.text.MessageFormat;
import org.apache.commons.logging.Log;
import net.osmand.LogUtil;
import net.osmand.plus.OsmandSettings;
import net.osmand.plus.R;
import org.apache.commons.logging.Log;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import android.content.Context;
public class LiveMonitoringHelper {
@ -40,19 +46,42 @@ public class LiveMonitoringHelper {
public void sendData(float lat, float lon, float alt, float speed, float hdop, long time) {
String url = MessageFormat.format(settings.LIVE_MONITORING_URL.get(), lat+"", lon+"", time+"", hdop+"", alt+"", speed+"");
try {
URL curl = new URL(url);
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, 15000);
DefaultHttpClient httpclient = new DefaultHttpClient(params);
HttpRequestBase method = new HttpGet(url);
log.info("Monitor " + url);
URLConnection conn = curl.openConnection();
conn.setDoOutput(false);
conn.connect();
InputStream is = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String r;
StringBuilder bs = new StringBuilder();
while((r=reader.readLine()) != null){
bs.append(r).append('\n');
HttpResponse response = httpclient.execute(method);
if(response.getStatusLine() == null ||
response.getStatusLine().getStatusCode() != 200){
String msg;
if(response.getStatusLine() != null){
msg = ctx.getString(R.string.failed_op); //$NON-NLS-1$
} else {
msg = response.getStatusLine().getStatusCode() + " : " + //$NON-NLS-1$//$NON-NLS-2$
response.getStatusLine().getReasonPhrase();
}
log.error("Error sending monitor request request : " + msg);
} else {
InputStream is = response.getEntity().getContent();
StringBuilder responseBody = new StringBuilder();
if (is != null) {
BufferedReader in = new BufferedReader(new InputStreamReader(is, "UTF-8")); //$NON-NLS-1$
String s;
while ((s = in.readLine()) != null) {
responseBody.append(s);
responseBody.append("\n"); //$NON-NLS-1$
}
is.close();
}
httpclient.getConnectionManager().shutdown();
log.info("Montior response : " + responseBody.toString());
}
is.close();
} catch (Exception e) {
log.error("Failed connect to " + url, e);
}