Add version to http requests, change post to get parameter

This commit is contained in:
Victor Shcherb 2016-08-24 11:49:14 +02:00
parent 77faa8d090
commit 0cb3914786
6 changed files with 39 additions and 22 deletions

View file

@ -30,6 +30,7 @@ public class AndroidNetworkUtils {
final Map<String, String> parameters,
final String userOperation,
final boolean toastAllowed,
final boolean post,
final OnRequestResultListener listener) {
new AsyncTask<Void, Void, String>() {
@ -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<String, String> 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<String, String> 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 {

View file

@ -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;

View file

@ -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<String, String> 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<Void, Void, String>() {
@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);

View file

@ -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<String, String> 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<Void, Void, String>() {
@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);

View file

@ -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) {

View file

@ -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();