From 0cb39147862fbbdef42c091d268a9d79e4f08050 Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Wed, 24 Aug 2016 11:49:14 +0200 Subject: [PATCH] Add version to http requests, change post to get parameter --- .../src/net/osmand/AndroidNetworkUtils.java | 26 ++++++++++--------- OsmAnd/src/net/osmand/plus/Version.java | 2 +- .../firstusage/FirstUsageWizardFragment.java | 16 +++++++++--- .../osmand/plus/helpers/DiscountHelper.java | 11 +++++--- .../net/osmand/plus/inapp/InAppHelper.java | 4 +-- .../liveupdates/SubscriptionFragment.java | 2 +- 6 files changed, 39 insertions(+), 22 deletions(-) diff --git a/OsmAnd/src/net/osmand/AndroidNetworkUtils.java b/OsmAnd/src/net/osmand/AndroidNetworkUtils.java index 9723aa9066..b80440f210 100644 --- a/OsmAnd/src/net/osmand/AndroidNetworkUtils.java +++ b/OsmAnd/src/net/osmand/AndroidNetworkUtils.java @@ -30,6 +30,7 @@ public class AndroidNetworkUtils { final Map parameters, final String userOperation, final boolean toastAllowed, + final boolean post, final OnRequestResultListener listener) { new AsyncTask() { @@ -37,7 +38,7 @@ public class AndroidNetworkUtils { @Override protected String doInBackground(Void... params) { try { - return sendRequest(ctx, url, parameters, userOperation, toastAllowed); + return sendRequest(ctx, url, parameters, userOperation, toastAllowed, post); } catch (Exception e) { return null; } @@ -55,15 +56,11 @@ public class AndroidNetworkUtils { public static String sendRequest(OsmandApplication ctx, String url, Map parameters, - String userOperation, boolean toastAllowed) { + String userOperation, boolean toastAllowed, boolean post) { HttpURLConnection connection = null; try { - connection = NetworkUtils.getHttpURLConnection(url); - - connection.setRequestProperty("Accept-Charset", "UTF-8"); - connection.setRequestProperty("User-Agent", Version.getFullVersion(ctx)); - connection.setConnectTimeout(15000); - + + String params = null; if (parameters != null && parameters.size() > 0) { StringBuilder sb = new StringBuilder(); for (Map.Entry entry : parameters.entrySet()) { @@ -72,13 +69,17 @@ public class AndroidNetworkUtils { } sb.append(entry.getKey()).append("=").append(URLEncoder.encode(entry.getValue(), "UTF-8")); } - String params = sb.toString(); - + params = sb.toString(); + } + connection = NetworkUtils.getHttpURLConnection(params == null || post ? url : url + "&" + params); + connection.setRequestProperty("Accept-Charset", "UTF-8"); + connection.setRequestProperty("User-Agent", Version.getFullVersion(ctx)); + connection.setConnectTimeout(15000); + if (params != null && post) { connection.setDoInput(true); connection.setDoOutput(true); connection.setUseCaches(false); connection.setRequestMethod("POST"); - connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8"); connection.setRequestProperty("Content-Length", String.valueOf(params.getBytes("UTF-8").length)); connection.setFixedLengthStreamingMode(params.getBytes("UTF-8").length); @@ -89,6 +90,7 @@ public class AndroidNetworkUtils { output.close(); } else { + connection.setRequestMethod("GET"); connection.connect(); } @@ -96,7 +98,7 @@ public class AndroidNetworkUtils { if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) { if (toastAllowed) { String msg = userOperation - + " " + ctx.getString(R.string.failed_op) + " : " + connection.getResponseMessage(); + + " " + ctx.getString(R.string.failed_op) + ": " + connection.getResponseMessage(); showToast(ctx, msg); } } else { diff --git a/OsmAnd/src/net/osmand/plus/Version.java b/OsmAnd/src/net/osmand/plus/Version.java index 2381850fb5..d30d0f16f4 100644 --- a/OsmAnd/src/net/osmand/plus/Version.java +++ b/OsmAnd/src/net/osmand/plus/Version.java @@ -53,7 +53,7 @@ public class Version { private static Version ver = null; private static Version getVersion(OsmandApplication ctx){ - if(ver == null){ + if (ver == null) { ver = new Version(ctx); } return ver; diff --git a/OsmAnd/src/net/osmand/plus/firstusage/FirstUsageWizardFragment.java b/OsmAnd/src/net/osmand/plus/firstusage/FirstUsageWizardFragment.java index f257193033..7de3fecbf9 100644 --- a/OsmAnd/src/net/osmand/plus/firstusage/FirstUsageWizardFragment.java +++ b/OsmAnd/src/net/osmand/plus/firstusage/FirstUsageWizardFragment.java @@ -6,6 +6,7 @@ import android.content.Intent; import android.os.AsyncTask; import android.os.Bundle; import android.os.StatFs; +import android.provider.Settings.Secure; import android.support.annotation.Nullable; import android.support.v4.app.ActivityCompat; import android.support.v4.app.Fragment; @@ -19,7 +20,6 @@ import android.view.ViewGroup; import android.widget.ImageButton; import android.widget.ProgressBar; import android.widget.TextView; - import net.osmand.AndroidNetworkUtils; import net.osmand.Location; import net.osmand.ValueHolder; @@ -33,6 +33,7 @@ import net.osmand.plus.OsmAndLocationProvider.OsmAndLocationListener; import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandSettings; import net.osmand.plus.R; +import net.osmand.plus.Version; import net.osmand.plus.activities.MapActivity; import net.osmand.plus.download.DownloadActivity; import net.osmand.plus.download.DownloadActivityType; @@ -53,9 +54,11 @@ import java.io.IOException; import java.text.MessageFormat; import java.util.ArrayList; import java.util.Iterator; +import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Locale; +import java.util.Map; import java.util.Timer; import java.util.TimerTask; @@ -326,13 +329,20 @@ public class FirstUsageWizardFragment extends Fragment implements OsmAndLocation switch (wizardType) { case SEARCH_LOCATION: if (searchLocationByIp) { + final Map pms = new LinkedHashMap<>(); + pms.put("version", Version.getFullVersion(app)); + try { + pms.put("aid", Secure.getString(app.getContentResolver(), Secure.ANDROID_ID)); + } catch (Exception e) { + e.printStackTrace(); + } new AsyncTask() { @Override protected String doInBackground(Void... params) { try { - return AndroidNetworkUtils.sendRequest(app, "http://osmand.net/api/geo-ip", null, - "Requesting location by IP...", false); + return AndroidNetworkUtils.sendRequest(app, "http://osmand.net/api/geo-ip", pms, + "Requesting location by IP...", false, false); } catch (Exception e) { logError("Requesting location by IP error: ", e); diff --git a/OsmAnd/src/net/osmand/plus/helpers/DiscountHelper.java b/OsmAnd/src/net/osmand/plus/helpers/DiscountHelper.java index b0c48f20ef..290d661dcf 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/DiscountHelper.java +++ b/OsmAnd/src/net/osmand/plus/helpers/DiscountHelper.java @@ -21,6 +21,7 @@ import android.annotation.SuppressLint; import android.content.Intent; import android.net.Uri; import android.os.AsyncTask; +import android.provider.Settings.Secure; import android.util.Log; import android.view.View; @@ -50,17 +51,21 @@ public class DiscountHelper { } mLastCheckTime = System.currentTimeMillis(); final Map pms = new LinkedHashMap<>(); - pms.put("name", Version.getAppName(app)); - pms.put("version", Version.getAppVersion(app)); + pms.put("version", Version.getFullVersion(app)); pms.put("nd", app.getAppInitializer().getFirstInstalledDays() +""); pms.put("ns", app.getAppInitializer().getNumberOfStarts() + ""); + try { + pms.put("aid", Secure.getString(app.getContentResolver(), Secure.ANDROID_ID)); + } catch (Exception e) { + e.printStackTrace(); + } new AsyncTask() { @Override protected String doInBackground(Void... params) { try { String res = AndroidNetworkUtils.sendRequest(mapActivity.getMyApplication(), - URL, pms, "Requesting discount info...", false); + URL, pms, "Requesting discount info...", false, false); return res; } catch (Exception e) { logError("Requesting discount info error: ", e); diff --git a/OsmAnd/src/net/osmand/plus/inapp/InAppHelper.java b/OsmAnd/src/net/osmand/plus/inapp/InAppHelper.java index 1329384af4..5011514b2d 100644 --- a/OsmAnd/src/net/osmand/plus/inapp/InAppHelper.java +++ b/OsmAnd/src/net/osmand/plus/inapp/InAppHelper.java @@ -300,7 +300,7 @@ public class InAppHelper { return AndroidNetworkUtils.sendRequest(ctx, "http://download.osmand.net/subscription/register.php", - parameters, "Requesting userId...", true); + parameters, "Requesting userId...", true, true); } catch (Exception e) { logError("sendRequest Error", e); @@ -443,7 +443,7 @@ public class InAppHelper { AndroidNetworkUtils.sendRequestAsync(ctx, "http://download.osmand.net/subscription/purchased.php", - parameters, "Sending purchase info...", true, new OnRequestResultListener() { + parameters, "Sending purchase info...", true, true, new OnRequestResultListener() { @Override public void onResult(String result) { if (result != null) { diff --git a/OsmAnd/src/net/osmand/plus/liveupdates/SubscriptionFragment.java b/OsmAnd/src/net/osmand/plus/liveupdates/SubscriptionFragment.java index 9bf0362f2a..30d522c0f9 100644 --- a/OsmAnd/src/net/osmand/plus/liveupdates/SubscriptionFragment.java +++ b/OsmAnd/src/net/osmand/plus/liveupdates/SubscriptionFragment.java @@ -229,7 +229,7 @@ public class SubscriptionFragment extends BaseOsmAndDialogFragment implements In AndroidNetworkUtils.sendRequestAsync(getMyApplication(), "http://download.osmand.net/subscription/update.php", - parameters, "Sending data...", true, new AndroidNetworkUtils.OnRequestResultListener() { + parameters, "Sending data...", true, true, new AndroidNetworkUtils.OnRequestResultListener() { @Override public void onResult(String result) { dismissProgress();