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; package net.osmand.plus.activities;
import java.io.BufferedInputStream;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.text.MessageFormat; import java.text.MessageFormat;
import org.apache.commons.logging.Log;
import net.osmand.LogUtil; import net.osmand.LogUtil;
import net.osmand.plus.OsmandSettings; 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; import android.content.Context;
public class LiveMonitoringHelper { 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) { 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+""); String url = MessageFormat.format(settings.LIVE_MONITORING_URL.get(), lat+"", lon+"", time+"", hdop+"", alt+"", speed+"");
try { 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); log.info("Monitor " + url);
URLConnection conn = curl.openConnection(); HttpResponse response = httpclient.execute(method);
conn.setDoOutput(false);
conn.connect(); if(response.getStatusLine() == null ||
InputStream is = conn.getInputStream(); response.getStatusLine().getStatusCode() != 200){
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String r; String msg;
StringBuilder bs = new StringBuilder(); if(response.getStatusLine() != null){
while((r=reader.readLine()) != null){ msg = ctx.getString(R.string.failed_op); //$NON-NLS-1$
bs.append(r).append('\n'); } 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) { } catch (Exception e) {
log.error("Failed connect to " + url, e); log.error("Failed connect to " + url, e);
} }