From e749aedd34091e17ebc65814fdcbcabcf41e1b24 Mon Sep 17 00:00:00 2001 From: Eric House Date: Wed, 15 Aug 2012 18:55:54 -0700 Subject: [PATCH] return url inside a json rather than just a url for more flexibility and so python's error messages don't trigger Notifications that then fail. --- .../src/org/eehouse/android/xw4/NetUtils.java | 23 +++++++++++++++++-- xwords4/android/scripts/info.py | 16 +++++++------ 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/NetUtils.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/NetUtils.java index 1e84ea322..205a03721 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/NetUtils.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/NetUtils.java @@ -48,6 +48,7 @@ import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; +import org.json.JSONObject; import org.eehouse.android.xw4.jni.CommonPrefs; @@ -287,6 +288,22 @@ public class NetUtils { } } // sendToRelay + private static String urlFromJson( String json ) + { + String result = null; + if ( null != json ) { + try { + JSONObject jobj = new JSONObject( json ); + if ( null != jobj && jobj.has( "url" ) ) { + result = jobj.getString( "url" ); + } + } catch ( org.json.JSONException jse ) { + DbgUtils.loge( jse ); + } + } + return result; + } + private static HttpPost makePost( String proc ) { return new HttpPost("http://www.eehouse.org/xw4/info.py/" + proc); @@ -327,7 +344,8 @@ public class NetUtils { nvp.add( new BasicNameValuePair( "name", dal.name ) ); nvp.add( new BasicNameValuePair( "lang", langStr ) ); nvp.add( new BasicNameValuePair( "md5sum", sum ) ); - return runPost( post, nvp ); + String json = runPost( post, nvp ); + return urlFromJson( json ); } public static void checkVersions( Context context ) @@ -349,7 +367,8 @@ public class NetUtils { nvp.add( new BasicNameValuePair( "version", String.format( "%d", versionCode ) ) ); - String url = runPost( post, nvp ); + String json = runPost( post, nvp ); + String url = urlFromJson( json ); if ( null != url ) { ApplicationInfo ai = pm.getApplicationInfo( packageName, 0); String label = pm.getApplicationLabel( ai ).toString(); diff --git a/xwords4/android/scripts/info.py b/xwords4/android/scripts/info.py index 1bd53a0fd..64d2082ef 100755 --- a/xwords4/android/scripts/info.py +++ b/xwords4/android/scripts/info.py @@ -3,7 +3,7 @@ # the client's version is up-to-date or with the newer version if it's # not. May include md5 sums in liu of versions for .xwd files. -import logging, shelve, hashlib, sys +import logging, shelve, hashlib, sys, json k_suffix = '.xwd' k_filebase = "/var/www/" @@ -56,21 +56,22 @@ def getDictSums(): # public def curVersion( req, name, version ): global k_versions - result = "" + result = {} logging.debug( "version: " + version ) if name in k_versions: if k_versions[name]['version'] > int(version): logging.debug( name + " is old" ) - result = k_urlbase + k_versions[name]['url'] + result['url'] = k_urlbase + k_versions[name]['url'] + result['success'] = True else: logging.debug(name + " is up-to-date") else: logging.debug( 'Error: bad name ' + name ) - return result + return json.dumps( result ) # public def dictVersion( req, name, lang, md5sum ): - result = '' + result = {} if not name.endswith(k_suffix): name += k_suffix dictSums = getDictSums() path = lang + "/" + name @@ -81,11 +82,12 @@ def dictVersion( req, name, lang, md5sum ): s_shelf['sums'] = dictSums if path in dictSums: if dictSums[path] != md5sum: - result = k_urlbase + "and_wordlists/" + path + result['url'] = k_urlbase + "and_wordlists/" + path + result['success'] = True else: logging.debug( path + " not known" ) s_shelf.close() - return result + return json.dumps( result ) def clearShelf(): shelf = shelve.open(k_shelfFile)