diff options
author | mucek4 <tomaz@gorenc.org> | 2011-09-22 12:06:41 +0200 |
---|---|---|
committer | mucek4 <tomaz@gorenc.org> | 2011-09-22 12:06:41 +0200 |
commit | 749626af7807bc61cc0238dfa4ea1036efcb8dc5 (patch) | |
tree | c4e50f4d22c7816a7ce42ead69023c8447a7b982 | |
parent | 9472b8c57d51d27a4ea3e26aade539612787d1d6 (diff) | |
parent | b41c7d190734309b666d478531202a148c70e4b0 (diff) | |
download | cgeo-749626af7807bc61cc0238dfa4ea1036efcb8dc5.zip cgeo-749626af7807bc61cc0238dfa4ea1036efcb8dc5.tar.gz cgeo-749626af7807bc61cc0238dfa4ea1036efcb8dc5.tar.bz2 |
Merge branch 'master' of ssh://github.com/cgeo/c-geo-opensource
-rw-r--r-- | main/src/cgeo/geocaching/CookieJar.java | 4 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/LogTemplateProvider.java | 5 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/cgBase.java | 187 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/cgGeo.java | 8 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/cgeoaddresses.java | 6 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/cgeocaches.java | 9 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/cgeoinit.java | 5 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/cgeotouch.java | 5 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/cgeovisit.java | 8 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/connector/GCConnector.java | 8 |
10 files changed, 129 insertions, 116 deletions
diff --git a/main/src/cgeo/geocaching/CookieJar.java b/main/src/cgeo/geocaching/CookieJar.java index 1b421d4..b6ccf8f 100644 --- a/main/src/cgeo/geocaching/CookieJar.java +++ b/main/src/cgeo/geocaching/CookieJar.java @@ -61,6 +61,10 @@ final public class CookieJar { static public synchronized void setCookies(final SharedPreferences prefs, final URLConnection uc) { final Map<String, List<String>> headers = uc.getHeaderFields(); + if (headers == null) { + // If a request failed, there might not be headers. + return; + } for (final Map.Entry<String, List<String>> entry : headers.entrySet()) { if ("set-cookie".equalsIgnoreCase(entry.getKey())) { for (final String field : entry.getValue()) { diff --git a/main/src/cgeo/geocaching/LogTemplateProvider.java b/main/src/cgeo/geocaching/LogTemplateProvider.java index 4119b37..76ba0e0 100644 --- a/main/src/cgeo/geocaching/LogTemplateProvider.java +++ b/main/src/cgeo/geocaching/LogTemplateProvider.java @@ -4,6 +4,7 @@ import org.apache.commons.lang3.StringUtils; import android.util.Log; +import java.net.URI; import java.util.HashMap; import java.util.Map; import java.util.regex.Matcher; @@ -14,6 +15,8 @@ import java.util.regex.Pattern; * */ public class LogTemplateProvider { + private static final URI URI_GC_EMAIL = cgBase.buildURI(false, "www.geocaching.com", "/email/"); + public static abstract class LogTemplate { private String template; private int resourceId; @@ -88,7 +91,7 @@ public class LogTemplateProvider { } String findCount = ""; final Map<String, String> params = new HashMap<String, String>(); - final String page = base.request(false, "www.geocaching.com", "/email/", "GET", params, false, false, false).getData(); + final String page = base.request(URI_GC_EMAIL, "GET", params, false, false, false).getData(); int current = parseFindCount(page); if (current >= 0) { diff --git a/main/src/cgeo/geocaching/cgBase.java b/main/src/cgeo/geocaching/cgBase.java index a5e953f..328a4d4 100644 --- a/main/src/cgeo/geocaching/cgBase.java +++ b/main/src/cgeo/geocaching/cgBase.java @@ -131,6 +131,17 @@ public class cgBase { private final static Pattern PATTERN_TRACKABLE_Distance = Pattern.compile("<h4[^>]*\\W*Tracking History \\(([0-9.,]+(km|mi))[^\\)]*\\)", Pattern.CASE_INSENSITIVE); private final static Pattern PATTERN_TRACKABLE_Log = Pattern.compile("<tr class=\"Data.+?src=\"/images/icons/([^.]+)\\.gif[^>]+> ([^<]+)</td>.+?guid.+?>([^<]+)</a>.+?(?:guid=([^\"]+)\">([^<]+)</a>.+?)?<td colspan=\"4\">(.+?)(?:<ul.+?ul>)?\\s*</td>\\s*</tr>", Pattern.CASE_INSENSITIVE); + private static final URI URI_GC_LOGIN_DEFAULT = buildURI(true, "www.geocaching.com", "/login/default.aspx"); + private static final URI URI_GC_DEFAULT = buildURI(false, "www.geocaching.com", "/default.aspx"); + private static final URI URI_GOOGLE_RECAPTCHA = buildURI(false, "www.google.com", "/recaptcha/api/challenge"); + private static final URI URI_GC_MAP_DEFAULT = buildURI(false, "www.geocaching.com", "/map/default.aspx"); + private static final URI URI_GC_SEEK_NEAREST = buildURI(false, "www.geocaching.com", "/seek/nearest.aspx"); + private static final URI URI_GC_SEEK_LOGBOOK = buildURI(false, "www.geocaching.com", "/seek/geocache.logbook"); + private static final URI URI_GC_PREFERENCES = buildURI(false, "www.geocaching.com", "/account/ManagePreferences.aspx"); + private static final URI URI_GCVOTE_GETVOTES = buildURI(false, "gcvote.com", "/getVotes.php"); + private static final URI URI_GO4CACHE_GET = buildURI(false, "api.go4cache.com", "/get.php"); + private static final URI URI_GC_TRACK_DETAILS = buildURI(false, "www.geocaching.com", "/track/details.aspx"); + public final static Map<String, String> cacheTypes = new HashMap<String, String>(); public final static Map<String, String> cacheIDs = new HashMap<String, String>(); static { @@ -470,8 +481,6 @@ public class cgBase { } public int login() { - final String host = "www.geocaching.com"; - final String path = "/login/default.aspx"; cgResponse loginResponse = null; String loginData = null; @@ -483,7 +492,7 @@ public class cgBase { return -3; // no login information stored } - loginResponse = request(true, host, path, "GET", new HashMap<String, String>(), false, false, false); + loginResponse = request(URI_GC_LOGIN_DEFAULT, "GET", new HashMap<String, String>(), false, false, false); loginData = loginResponse.getData(); if (StringUtils.isNotBlank(loginData)) { if (checkLogin(loginData)) { @@ -523,7 +532,7 @@ public class cgBase { params.put("ctl00$SiteContent$cbRememberMe", "on"); params.put("ctl00$SiteContent$btnSignIn", "Login"); - loginResponse = request(true, host, path, "POST", params, false, false, false); + loginResponse = request(URI_GC_LOGIN_DEFAULT, "POST", params, false, false, false); loginData = loginResponse.getData(); if (StringUtils.isNotBlank(loginData)) { @@ -582,15 +591,13 @@ public class cgBase { } public String switchToEnglish(String[] viewstates) { - final String host = "www.geocaching.com"; - final String path = "/default.aspx"; final Map<String, String> params = new HashMap<String, String>(); setViewstates(viewstates, params); params.put("__EVENTTARGET", "ctl00$uxLocaleList$uxLocaleList$ctl00$uxLocaleItem"); // switch to english params.put("__EVENTARGUMENT", ""); - return request(false, host, path, "POST", params, false, false, false).getData(); + return request(URI_GC_DEFAULT, "POST", params, false, false, false).getData(); } public cgCacheWrap parseSearch(cgSearchThread thread, String url, String page, boolean showCaptcha) { @@ -633,7 +640,7 @@ public class cgBase { } if (recaptchaJsParam != null) { - final String recaptchaJs = request(false, "www.google.com", "/recaptcha/api/challenge", "GET", "k=" + urlencode_rfc3986(recaptchaJsParam.trim()), 0, true).getData(); + final String recaptchaJs = request(URI_GOOGLE_RECAPTCHA, "GET", "k=" + urlencode_rfc3986(recaptchaJsParam.trim()), 0, true).getData(); if (StringUtils.isNotBlank(recaptchaJs)) { final Matcher matcherRecaptchaChallenge = patternRecaptchaChallenge.matcher(recaptchaJs); @@ -884,8 +891,6 @@ public class cgBase { try { // get coordinates for parsed caches - final String host = "www.geocaching.com"; - final String path = "/seek/nearest.aspx"; final StringBuilder params = new StringBuilder(); params.append("__EVENTTARGET=&__EVENTARGUMENT="); if (ArrayUtils.isNotEmpty(caches.viewstates)) { @@ -912,7 +917,7 @@ public class cgBase { } params.append("&ctl00%24ContentBody%24uxDownloadLoc=Download+Waypoints"); - final String coordinates = request(false, host, path, "POST", params.toString(), 0, true).getData(); + final String coordinates = request(URI_GC_SEEK_NEAREST, "POST", params.toString(), 0, true).getData(); if (StringUtils.isNotBlank(coordinates)) { if (coordinates.contains("You have not agreed to the license agreement. The license agreement is required before you can start downloading GPX or LOC files from Geocaching.com")) { @@ -967,14 +972,14 @@ public class cgBase { return caches; } - public static cgCacheWrap parseMapJSON(String url, String data) { + public static cgCacheWrap parseMapJSON(final URI uri, final String data) { if (StringUtils.isEmpty(data)) { Log.e(cgSettings.tag, "cgeoBase.parseMapJSON: No page given"); return null; } final cgCacheWrap caches = new cgCacheWrap(); - caches.url = url; + caches.url = uri.toString(); try { final JSONObject yoDawg = new JSONObject(data); @@ -1667,8 +1672,7 @@ public class cgBase { params.put("sp", "0"); params.put("sf", "0"); params.put("decrypt", "1"); - final cgResponse response = request(false, "www.geocaching.com", "/seek/geocache.logbook", "GET", - params, false, false, false); + final cgResponse response = request(URI_GC_SEEK_LOGBOOK, "GET", params, false, false, false); if (response.getStatusCode() != 200) { Log.e(cgSettings.tag, "cgBase.loadLogsFromDetails: error " + response.getStatusCode() + " when requesting log information"); return; @@ -1816,10 +1820,7 @@ public class cgBase { public void detectGcCustomDate() { - final String host = "www.geocaching.com"; - final String path = "/account/ManagePreferences.aspx"; - - final String result = request(false, host, path, "GET", null, false, false, false).getData(); + final String result = request(URI_GC_PREFERENCES, "GET", null, false, false, false).getData(); if (null == result) { Log.w(cgSettings.tag, "cgeoBase.detectGcCustomDate: result is null"); @@ -1881,7 +1882,7 @@ public class cgBase { params.put("waypoints", implode(",", geocodes.toArray())); } params.put("version", "cgeo"); - final String votes = request(false, "gcvote.com", "/getVotes.php", "GET", params, false, false, false).getData(); + final String votes = request(URI_GCVOTE_GETVOTES, "GET", params, false, false, false).getData(); if (votes == null) { return null; } @@ -2524,11 +2525,12 @@ public class cgBase { params.put("__EVENTTARGET", "ctl00$ContentBody$pgrBottom$ctl08"); params.put("__EVENTARGUMENT", ""); - String page = request(false, host, path, method, params, false, false, true).getData(); + final URI uri = buildURI(false, host, path); + String page = request(uri, method, params, false, false, true).getData(); if (checkLogin(page) == false) { int loginState = login(); if (loginState == 1) { - page = request(false, host, path, method, params, false, false, true).getData(); + page = request(uri, method, params, false, false, true).getData(); } else if (loginState == -3) { Log.i(cgSettings.tag, "Working as guest."); } else { @@ -2673,7 +2675,8 @@ public class cgBase { params.put("lng", longitude); final String url = "http://" + host + path + "?" + prepareParameters(params, false, true); - String page = requestLogged(false, host, path, method, params, false, false, true); + final URI uri = buildURI(false, host, path); + String page = requestLogged(uri, method, params, false, false, true); if (StringUtils.isBlank(page)) { Log.e(cgSettings.tag, "cgeoBase.searchByCoords: No data from server"); @@ -2723,7 +2726,8 @@ public class cgBase { params.put("key", keyword); final String url = "http://" + host + path + "?" + prepareParameters(params, false, true); - String page = requestLogged(false, host, path, method, params, false, false, true); + final URI uri = cgBase.buildURI(false, host, path); + String page = requestLogged(uri, method, params, false, false, true); if (StringUtils.isBlank(page)) { Log.e(cgSettings.tag, "cgeoBase.searchByKeyword: No data from server"); @@ -2779,7 +2783,8 @@ public class cgBase { } final String url = "http://" + host + path + "?" + prepareParameters(params, my, true); - String page = requestLogged(false, host, path, method, params, false, my, true); + final URI uri = buildURI(false, host, path); + String page = requestLogged(uri, method, params, false, my, true); if (StringUtils.isBlank(page)) { Log.e(cgSettings.tag, "cgeoBase.searchByUsername: No data from server"); @@ -2829,7 +2834,8 @@ public class cgBase { params.put("u", userName); final String url = "http://" + host + path + "?" + prepareParameters(params, false, true); - String page = requestLogged(false, host, path, method, params, false, false, true); + final URI uri = buildURI(false, host, path); + String page = requestLogged(uri, method, params, false, false, true); if (StringUtils.isBlank(page)) { Log.e(cgSettings.tag, "cgeoBase.searchByOwner: No data from server"); @@ -2879,15 +2885,15 @@ public class cgBase { String params = "{\"dto\":{\"data\":{\"c\":1,\"m\":\"\",\"d\":\"" + latMax + "|" + latMin + "|" + lonMax + "|" + lonMin + "\"},\"ut\":\"" + usertoken + "\"}}"; - final String url = "http://" + host + path + "?" + params; - page = requestJSONgc(host, path, params); + final URI uri = buildURI(false, host, path); + page = requestJSONgc(uri, params); if (StringUtils.isBlank(page)) { Log.e(cgSettings.tag, "cgeoBase.searchByViewport: No data from server"); return null; } - final cgCacheWrap caches = parseMapJSON(url, page); + final cgCacheWrap caches = parseMapJSON(buildURI(false, host, path, params), page); if (caches == null || caches.cacheList == null || caches.cacheList.isEmpty()) { Log.e(cgSettings.tag, "cgeoBase.searchByViewport: No cache parsed"); } @@ -2914,9 +2920,6 @@ public class cgBase { return users; } - final String host = "api.go4cache.com"; - final String path = "/get.php"; - final String method = "POST"; final Map<String, String> params = new HashMap<String, String>(); params.put("u", username); @@ -2925,7 +2928,7 @@ public class cgBase { params.put("lnm", String.format((Locale) null, "%.6f", lonMin)); params.put("lnx", String.format((Locale) null, "%.6f", lonMax)); - final String data = request(false, host, path, method, params, false, false, false).getData(); + final String data = request(URI_GO4CACHE_GET, "POST", params, false, false, false).getData(); if (StringUtils.isBlank(data)) { Log.e(cgSettings.tag, "cgeoBase.getGeocachersInViewport: No data from server"); @@ -3005,9 +3008,6 @@ public class cgBase { return null; } - final String host = "www.geocaching.com"; - final String path = "/track/details.aspx"; - final String method = "GET"; final Map<String, String> params = new HashMap<String, String>(); if (StringUtils.isNotBlank(geocode)) { params.put("tracker", geocode); @@ -3017,7 +3017,7 @@ public class cgBase { params.put("id", id); } - String page = requestLogged(false, host, path, method, params, false, false, false); + String page = requestLogged(URI_GC_TRACK_DETAILS, "GET", params, false, false, false); if (StringUtils.isBlank(page)) { Log.e(cgSettings.tag, "cgeoBase.searchTrackable: No data from server"); @@ -3075,9 +3075,6 @@ public class cgBase { Log.i(cgSettings.tag, "Trying to post log for cache #" + cacheid + " - action: " + logType + "; date: " + year + "." + month + "." + day + ", log: " + log + "; trackables: 0"); } - final String host = "www.geocaching.com"; - final String path = "/seek/log.aspx?ID=" + cacheid; - final String method = "POST"; final Map<String, String> params = new HashMap<String, String>(); setViewstates(viewstates, params); @@ -3108,11 +3105,12 @@ public class cgBase { params.put("ctl00$ContentBody$LogBookPanel1$uxTrackables$hdnCurrentFilter", ""); } - String page = request(false, host, path, method, params, false, false, false).getData(); + final URI uri = buildURI(false, "www.geocaching.com", "/seek/log.aspx", "ID=" + cacheid); + String page = request(uri, "POST", params, false, false, false).getData(); if (checkLogin(page) == false) { int loginState = login(); if (loginState == 1) { - page = request(false, host, path, method, params, false, false, false).getData(); + page = request(uri, "POST", params, false, false, false).getData(); } else { Log.e(cgSettings.tag, "cgeoBase.postLog: Can not log in geocaching (error: " + loginState + ")"); return loginState; @@ -3169,7 +3167,7 @@ public class cgBase { params.put("ctl00$ContentBody$LogBookPanel1$uxTrackables$hdnCurrentFilter", ""); } - page = request(false, host, path, method, params, false, false, false).getData(); + page = request(uri, "POST", params, false, false, false).getData(); } } catch (Exception e) { Log.e(cgSettings.tag, "cgeoBase.postLog.confim: " + e.toString()); @@ -3217,9 +3215,6 @@ public class cgBase { log = log.replace("\n", "\r\n"); // windows' eol final Calendar currentDate = Calendar.getInstance(); - final String host = "www.geocaching.com"; - final String path = "/track/log.aspx?wid=" + tbid; - final String method = "POST"; final Map<String, String> params = new HashMap<String, String>(); setViewstates(viewstates, params); @@ -3240,11 +3235,12 @@ public class cgBase { params.put("ctl00$ContentBody$LogBookPanel1$LogButton", "Submit Log Entry"); params.put("ctl00$ContentBody$uxVistOtherListingGC", ""); - String page = request(false, host, path, method, params, false, false, false).getData(); + final URI uri = buildURI(false, "www.geocaching.com", "/track/log.aspx", "wid=" + tbid); + String page = request(uri, "POST", params, false, false, false).getData(); if (checkLogin(page) == false) { int loginState = login(); if (loginState == 1) { - page = request(false, host, path, method, params, false, false, false).getData(); + page = request(uri, "POST", params, false, false, false).getData(); } else { Log.e(cgSettings.tag, "cgeoBase.postLogTrackable: Can not log in geocaching (error: " + loginState + ")"); return loginState; @@ -3279,7 +3275,8 @@ public class cgBase { * @return -1: error occured */ public int addToWatchlist(cgCache cache) { - String page = requestLogged(false, "www.geocaching.com", "/my/watchlist.aspx?w=" + cache.cacheId, + final URI uri = buildURI(false, "www.geocaching.com", "/my/watchlist.aspx", "w=" + cache.cacheId); + String page = requestLogged(uri, "POST", null, false, false, false); if (StringUtils.isBlank(page)) { @@ -3305,11 +3302,8 @@ public class cgBase { * @return -1: error occured */ public int removeFromWatchlist(cgCache cache) { - String host = "www.geocaching.com"; - String path = "/my/watchlist.aspx?ds=1&action=rem&id=" + cache.cacheId; - String method = "POST"; - - String page = requestLogged(false, host, path, method, null, false, false, false); + final URI uri = buildURI(false, "www.geocaching.com", "/my/watchlist.aspx", "ds=1&action=rem&id=" + cache.cacheId); + String page = requestLogged(uri, "POST", null, false, false, false); if (StringUtils.isBlank(page)) { Log.e(cgSettings.tag, "cgBase.removeFromWatchlist: No data from server"); @@ -3323,7 +3317,7 @@ public class cgBase { params.put("__EVENTARGUMENT", ""); params.put("ctl00$ContentBody$btnYes", "Yes"); - page = request(false, host, path, method, params, false, false, false).getData(); + page = request(uri, "POST", params, false, false, false).getData(); boolean guidOnPage = cache.isGuidContainedInPage(page); if (!guidOnPage) { Log.i(cgSettings.tag, "cgBase.removeFromWatchlist: cache removed from watchlist"); @@ -3569,20 +3563,35 @@ public class cgBase { return paramsDone; } - public String[] requestViewstates(boolean secure, String host, String path, String method, Map<String, String> params, boolean xContentType, boolean my) { - final cgResponse response = request(secure, host, path, method, params, xContentType, my, false); + public static URI buildURI(final boolean secure, final String host, final String path, final String query) { + try { + return new URI(secure ? "https" : "http", host, path, query, null); + } catch (URISyntaxException e) { + // This should never happen as we only use trusted URI components. + Log.e(cgSettings.tag, "buildURI", e); + return null; + } + } + + public static URI buildURI(boolean secure, String host, String path) { + final String pathComponents[] = StringUtils.split(path, "?", 2); + return buildURI(secure, host, pathComponents[0], pathComponents.length > 1 ? pathComponents[1] : null); + } + + public String[] requestViewstates(final URI uri, String method, Map<String, String> params, boolean xContentType, boolean my) { + final cgResponse response = request(uri, method, params, xContentType, my, false); return getViewstates(response.getData()); } - public String requestLogged(boolean secure, String host, String path, String method, Map<String, String> params, boolean xContentType, boolean my, boolean addF) { - cgResponse response = request(secure, host, path, method, params, xContentType, my, addF); + public String requestLogged(final URI uri, String method, Map<String, String> params, boolean xContentType, boolean my, boolean addF) { + cgResponse response = request(uri, method, params, xContentType, my, addF); String data = response.getData(); if (checkLogin(data) == false) { int loginState = login(); if (loginState == 1) { - response = request(secure, host, path, method, params, xContentType, my, addF); + response = request(uri, method, params, xContentType, my, addF); data = response.getData(); } else { Log.i(cgSettings.tag, "Working as guest."); @@ -3592,21 +3601,18 @@ public class cgBase { return data; } - public cgResponse request(boolean secure, String host, String path, String method, Map<String, String> params, boolean xContentType, boolean my, boolean addF) { + public cgResponse request(final URI uri, String method, Map<String, String> params, boolean xContentType, boolean my, boolean addF) { // prepare parameters final String paramsDone = prepareParameters(params, my, addF); - - return request(secure, host, path, method, paramsDone, 0, xContentType); + return request(uri, method, paramsDone, 0, xContentType); } - public cgResponse request(boolean secure, String host, String path, String method, Map<String, String> params, int requestId, boolean xContentType, boolean my, boolean addF) { - // prepare parameters + public cgResponse request(final URI uri, String method, Map<String, String> params, int requestId, boolean xContentType, boolean my, boolean addF) { final String paramsDone = prepareParameters(params, my, addF); - - return request(secure, host, path, method, paramsDone, requestId, xContentType); + return request(uri, method, paramsDone, requestId, xContentType); } - public cgResponse request(boolean secure, String host, String path, String method, String params, int requestId, Boolean xContentType) { + public cgResponse request(final URI uri, String method, String params, int requestId, Boolean xContentType) { URL u = null; int httpCode = -1; String httpMessage = null; @@ -3622,12 +3628,6 @@ public class cgBase { method = method.toUpperCase(); } - // https - String scheme = "http://"; - if (secure) { - scheme = "https://"; - } - String cookiesDone = CookieJar.getCookiesAsString(prefs); URLConnection uc = null; @@ -3646,10 +3646,10 @@ public class cgBase { try { if (method.equals("GET")) { // GET - u = new URL(scheme + host + path + "?" + params); + u = new URI(uri.getScheme(), uri.getAuthority(), uri.getPath(), params, null).toURL(); uc = u.openConnection(); - uc.setRequestProperty("Host", host); + uc.setRequestProperty("Host", uri.getHost()); uc.setRequestProperty("Cookie", cookiesDone); if (xContentType) { uc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); @@ -3673,10 +3673,10 @@ public class cgBase { connection.setDoOutput(false); } else { // POST - u = new URL(scheme + host + path); + u = uri.toURL(); uc = u.openConnection(); - uc.setRequestProperty("Host", host); + uc.setRequestProperty("Host", uri.getHost()); uc.setRequestProperty("Cookie", cookiesDone); if (xContentType) { uc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); @@ -3719,16 +3719,16 @@ public class cgBase { httpLocation = uc.getHeaderField("Location"); final String paramsLog = params.replaceAll(passMatch, "password=***"); - Log.i(cgSettings.tag + "|" + requestId, "[" + method + " " + (int) (params.length() / 1024) + "k | " + httpCode + " | " + (int) (buffer.length() / 1024) + "k] Downloaded " + scheme + host + path + "?" + paramsLog); + Log.i(cgSettings.tag + "|" + requestId, "[" + method + " " + (int) (params.length() / 1024) + "k | " + httpCode + " | " + (int) (buffer.length() / 1024) + "k] Downloaded " + uri + "?" + paramsLog); connection.disconnect(); br.close(); ins.close(); inr.close(); } catch (IOException e) { - Log.e(cgSettings.tag, "cgeoBase.request.IOException: " + e.toString()); + Log.e(cgSettings.tag, "cgeoBase.request.IOException", e); } catch (Exception e) { - Log.e(cgSettings.tag, "cgeoBase.request: " + e.toString()); + Log.e(cgSettings.tag, "cgeoBase.request", e); } if (buffer.length() > 0) { @@ -3740,9 +3740,8 @@ public class cgBase { try { if (httpCode == 302 && httpLocation != null) { - final URI newLocation = new URI(scheme, host, path, null).resolve(httpLocation); - response = request(newLocation.getScheme() == "https", - newLocation.getHost(), newLocation.getPath(), + final URI newLocation = uri.resolve(httpLocation); + response = request(newLocation, "GET", new HashMap<String, String>(), requestId, false, false, false); } else { if (StringUtils.isNotEmpty(buffer)) { @@ -3794,7 +3793,7 @@ public class cgBase { buffer.append(chars); } - public String requestJSONgc(String host, String path, String params) { + public String requestJSONgc(final URI uri, String params) { int httpCode = -1; String httpLocation = null; @@ -3815,15 +3814,15 @@ public class cgBase { try { // POST - final URL u = new URL("http://" + host + path); + final URL u = uri.toURL(); uc = u.openConnection(); - uc.setRequestProperty("Host", host); + uc.setRequestProperty("Host", uri.getHost()); uc.setRequestProperty("Cookie", cookiesDone); uc.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); uc.setRequestProperty("X-Requested-With", "XMLHttpRequest"); uc.setRequestProperty("Accept", "application/json, text/javascript, */*; q=0.01"); - uc.setRequestProperty("Referer", host + "/" + path); + uc.setRequestProperty("Referer", uri.getHost() + "/" + uri.getPath()); if (settings.asBrowser == 1) { uc.setRequestProperty("Accept-Charset", "utf-8, iso-8859-1, utf-16, *;q=0.7"); @@ -3858,7 +3857,7 @@ public class cgBase { httpLocation = uc.getHeaderField("Location"); final String paramsLog = params.replaceAll(passMatch, "password=***"); - Log.i(cgSettings.tag + " | JSON", "[POST " + (int) (params.length() / 1024) + "k | " + httpCode + " | " + (int) (buffer.length() / 1024) + "k] Downloaded " + "http://" + host + path + "?" + paramsLog); + Log.i(cgSettings.tag + " | JSON", "[POST " + (int) (params.length() / 1024) + "k | " + httpCode + " | " + (int) (buffer.length() / 1024) + "k] Downloaded " + uri.toString() + "?" + paramsLog); connection.disconnect(); br.close(); @@ -3877,12 +3876,8 @@ public class cgBase { String page = null; if (httpCode == 302 && httpLocation != null) { - try { - final URI newLocation = new URI("http", host, path, null).resolve(httpLocation); - page = requestJSONgc(newLocation.getHost(), newLocation.getPath(), params); - } catch (URISyntaxException e) { - Log.e(cgSettings.tag, "requestJSONgc", e); - } + final URI newLocation = uri.resolve(httpLocation); + page = requestJSONgc(newLocation, params); } else { replaceWhitespace(buffer); page = buffer.toString(); @@ -4500,7 +4495,7 @@ public class cgBase { } public String getMapUserToken(Handler noTokenHandler) { - final cgResponse response = request(false, "www.geocaching.com", "/map/default.aspx", "GET", "", 0, false); + final cgResponse response = request(URI_GC_MAP_DEFAULT, "GET", "", 0, false); final String data = response.getData(); String usertoken = null; diff --git a/main/src/cgeo/geocaching/cgGeo.java b/main/src/cgeo/geocaching/cgGeo.java index c4f51f3..c458108 100644 --- a/main/src/cgeo/geocaching/cgGeo.java +++ b/main/src/cgeo/geocaching/cgGeo.java @@ -15,6 +15,7 @@ import android.location.LocationManager; import android.os.Bundle; import android.util.Log; +import java.net.URI; import java.util.HashMap; import java.util.Iterator; import java.util.Locale; @@ -51,6 +52,8 @@ public class cgGeo { public Integer satellitesFixed = null; public double distanceNow = 0d; + private static final URI URI_GO4CACHE = cgBase.buildURI(false, "api.go4cache.com", "/"); + public cgGeo(Context contextIn, cgeoapplication appIn, cgUpdateLoc geoUpdateIn, cgBase baseIn, cgSettings settingsIn, int timeIn, int distanceIn) { context = contextIn; app = appIn; @@ -381,9 +384,6 @@ public class cgGeo { if (settings.publicLoc == 1 && (lastGo4cacheCoords == null || coordsNow.distanceTo(lastGo4cacheCoords) > 0.75)) { g4cRunning = true; - final String host = "api.go4cache.com"; - final String path = "/"; - final String method = "POST"; String action = null; if (app != null) { action = app.getAction(); @@ -404,7 +404,7 @@ public class cgGeo { if (base.version != null) { params.put("v", base.version); } - final String res = base.request(false, host, path, method, params, false, false, false).getData(); + final String res = base.request(URI_GO4CACHE, "POST", params, false, false, false).getData(); if (StringUtils.isNotBlank(res)) { lastGo4cacheCoords = coordsNow; diff --git a/main/src/cgeo/geocaching/cgeoaddresses.java b/main/src/cgeo/geocaching/cgeoaddresses.java index 0261349..ffede1e 100644 --- a/main/src/cgeo/geocaching/cgeoaddresses.java +++ b/main/src/cgeo/geocaching/cgeoaddresses.java @@ -139,11 +139,9 @@ public class cgeoaddresses extends AbstractActivity { List<Address> knownLocations = geocoder.getFromLocationName(keyword, 20); addresses.clear(); - for (Address address : knownLocations) { - addresses.add(address); - } + addresses.addAll(knownLocations); } catch (Exception e) { - Log.e(cgSettings.tag, "cgeoaddresses.loadPlaces.run: " + e.toString()); + Log.e(cgSettings.tag, "cgeoaddresses.loadPlaces.run", e); } loadPlacesHandler.sendMessage(new Message()); diff --git a/main/src/cgeo/geocaching/cgeocaches.java b/main/src/cgeo/geocaching/cgeocaches.java index 4d864ca..3e12b2d 100644 --- a/main/src/cgeo/geocaching/cgeocaches.java +++ b/main/src/cgeo/geocaching/cgeocaches.java @@ -41,6 +41,7 @@ import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.ContextMenu; +import android.view.ContextMenu.ContextMenuInfo; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.Menu; @@ -48,12 +49,11 @@ import android.view.MenuItem; import android.view.SubMenu; import android.view.View; import android.view.WindowManager; -import android.view.ContextMenu.ContextMenuInfo; +import android.widget.AdapterView.AdapterContextMenuInfo; import android.widget.EditText; import android.widget.ListView; import android.widget.RelativeLayout; import android.widget.TextView; -import android.widget.AdapterView.AdapterContextMenuInfo; import java.io.File; import java.io.FileOutputStream; @@ -61,6 +61,7 @@ import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.Writer; +import java.net.URI; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collections; @@ -140,6 +141,8 @@ public class cgeocaches extends AbstractListActivity { private static final int CONTEXT_MENU_MOVE_TO_LIST = 1000; private static final int MENU_MOVE_SELECTED_OR_ALL_TO_LIST = 1200; + private static final URI URI_SEND2CGEO_READ = cgBase.buildURI(false, "send2.cgeo.org", "/read.html"); + private String action = null; private String type = null; private Geopoint coords = null; @@ -2083,7 +2086,7 @@ public class cgeocaches extends AbstractListActivity { if (deviceCode == null) { deviceCode = ""; } - cgResponse responseFromWeb = base.request(false, "send2.cgeo.org", "/read.html", "GET", "code=" + cgBase.urlencode_rfc3986(deviceCode), 0, true); + cgResponse responseFromWeb = base.request(URI_SEND2CGEO_READ, "GET", "code=" + cgBase.urlencode_rfc3986(deviceCode), 0, true); if (responseFromWeb.getStatusCode() == 200) { if (responseFromWeb.getData().length() > 2) { diff --git a/main/src/cgeo/geocaching/cgeoinit.java b/main/src/cgeo/geocaching/cgeoinit.java index 6a1c7ea..cbce65a 100644 --- a/main/src/cgeo/geocaching/cgeoinit.java +++ b/main/src/cgeo/geocaching/cgeoinit.java @@ -31,11 +31,14 @@ import android.widget.Spinner; import android.widget.TextView; import java.io.File; +import java.net.URI; public class cgeoinit extends AbstractActivity { private final int SELECT_MAPFILE_REQUEST = 1; + private static final URI URI_SEND2CGEO_AUTH = cgBase.buildURI(false, "send2.cgeo.org", "/auth.html"); + private ProgressDialog loginDialog = null; private ProgressDialog webDialog = null; private Handler logInHandler = new Handler() { @@ -1046,7 +1049,7 @@ public class cgeoinit extends AbstractActivity { String params = "name=" + cgBase.urlencode_rfc3986(nam) + "&code=" + cgBase.urlencode_rfc3986(cod); - cgResponse response = base.request(false, "send2.cgeo.org", "/auth.html", "GET", params, 0, true); + cgResponse response = base.request(URI_SEND2CGEO_AUTH, "GET", params, 0, true); if (response.getStatusCode() == 200) { diff --git a/main/src/cgeo/geocaching/cgeotouch.java b/main/src/cgeo/geocaching/cgeotouch.java index 5943ef2..5c62087 100644 --- a/main/src/cgeo/geocaching/cgeotouch.java +++ b/main/src/cgeo/geocaching/cgeotouch.java @@ -20,6 +20,7 @@ import android.widget.EditText; import android.widget.LinearLayout; import android.widget.TextView; +import java.net.URI; import java.util.ArrayList; import java.util.Calendar; import java.util.HashMap; @@ -27,6 +28,8 @@ import java.util.List; import java.util.Map; public class cgeotouch extends cgLogForm { + private static final URI URI_GC_TRACK_LOG = cgBase.buildURI(false, "www.geocaching.com", "/track/log.aspx"); + private cgTrackable trackable = null; private List<Integer> types = new ArrayList<Integer>(); private ProgressDialog waitDialog = null; @@ -386,7 +389,7 @@ public class cgeotouch extends cgLogForm { return; } - final String page = base.request(false, "www.geocaching.com", "/track/log.aspx", "GET", params, false, false, false).getData(); + final String page = base.request(URI_GC_TRACK_LOG, "GET", params, false, false, false).getData(); viewstates = cgBase.getViewstates(page); diff --git a/main/src/cgeo/geocaching/cgeovisit.java b/main/src/cgeo/geocaching/cgeovisit.java index 3cba036..9689e96 100644 --- a/main/src/cgeo/geocaching/cgeovisit.java +++ b/main/src/cgeo/geocaching/cgeovisit.java @@ -25,6 +25,7 @@ import android.widget.EditText; import android.widget.LinearLayout; import android.widget.TextView; +import java.net.URI; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; @@ -42,6 +43,8 @@ public class cgeovisit extends cgLogForm { private static final int MENU_SIGNATURE = 1; private static final int SUBMENU_VOTE = 2; + private static final URI URI_GC_SEEK_LOG = cgBase.buildURI(false, "www.geocaching.com", "/seek/log.aspx"); + private LayoutInflater inflater = null; private cgCache cache = null; private List<Integer> types = new ArrayList<Integer>(); @@ -220,6 +223,7 @@ public class cgeovisit extends cgLogForm { } } }; + private static final URI URI_GCVOTE_SETVOTE = cgBase.buildURI(false, "gcvote.com", "/setVote.php"); public cgeovisit() { super("c:geo-log"); @@ -381,7 +385,7 @@ public class cgeovisit extends cgLogForm { params.put("voteUser", String.format("%.1f", rating).replace(',', '.')); params.put("version", "cgeo"); - final String result = base.request(false, "gcvote.com", "/setVote.php", "GET", params, false, false, false).getData(); + final String result = base.request(URI_GCVOTE_SETVOTE, "GET", params, false, false, false).getData(); return result.trim().equalsIgnoreCase("ok"); } @@ -713,7 +717,7 @@ public class cgeovisit extends cgLogForm { return; } - final String page = base.request(false, "www.geocaching.com", "/seek/log.aspx", "GET", params, false, false, false).getData(); + final String page = base.request(URI_GC_SEEK_LOG, "GET", params, false, false, false).getData(); viewstates = cgBase.getViewstates(page); trackables = cgBase.parseTrackableLog(page); diff --git a/main/src/cgeo/geocaching/connector/GCConnector.java b/main/src/cgeo/geocaching/connector/GCConnector.java index 2eea539..3aca3d7 100644 --- a/main/src/cgeo/geocaching/connector/GCConnector.java +++ b/main/src/cgeo/geocaching/connector/GCConnector.java @@ -11,6 +11,7 @@ import org.apache.commons.lang3.StringUtils; import android.util.Log; +import java.net.URI; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -19,6 +20,8 @@ import java.util.UUID; public class GCConnector extends AbstractConnector implements IConnector { + private static final URI URI_GC_SEEK_CACHE_DETAILS = cgBase.buildURI(false, "www.geocaching.com", "/seek/cache_details.aspx"); + @Override public boolean canHandle(String geocode) { return StringUtils.isNotBlank(geocode) && StringUtils.startsWithIgnoreCase(geocode, "GC"); @@ -66,9 +69,6 @@ public class GCConnector extends AbstractConnector implements IConnector { @Override public UUID searchByGeocode(final cgBase base, String geocode, final String guid, final cgeoapplication app, final cgSearch search, final int reason) { - final String host = "www.geocaching.com"; - final String path = "/seek/cache_details.aspx"; - final String method = "GET"; final Map<String, String> params = new HashMap<String, String>(); if (StringUtils.isNotBlank(geocode)) { params.put("wp", geocode); @@ -77,7 +77,7 @@ public class GCConnector extends AbstractConnector implements IConnector { } params.put("decrypt", "y"); - String page = base.requestLogged(false, host, path, method, params, false, false, false); + String page = base.requestLogged(URI_GC_SEEK_CACHE_DETAILS, "GET", params, false, false, false); if (StringUtils.isEmpty(page)) { if (app.isThere(geocode, guid, true, false)) { |