From a45b698ea902ddca773ad9e492a0441462e31451 Mon Sep 17 00:00:00 2001 From: Eric House Date: Mon, 3 Aug 2015 05:25:10 -0700 Subject: [PATCH] don't crash when first run without network connection --- .../eehouse/android/xw4/DictsDelegate.java | 79 ++++++++++--------- .../src/org/eehouse/android/xw4/NetUtils.java | 2 + 2 files changed, 43 insertions(+), 38 deletions(-) diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictsDelegate.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictsDelegate.java index 9fdc0e99d..268451827 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictsDelegate.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictsDelegate.java @@ -1140,50 +1140,53 @@ public class DictsDelegate extends ListDelegateBase JSONObject theOne = null; String langName = null; String json = NetUtils.runPost( post, new JSONObject() ); - try { - JSONObject obj = new JSONObject( json ); - JSONArray langs = obj.optJSONArray( "langs" ); - int nLangs = langs.length(); - for ( int ii = 0; ii < nLangs; ++ii ) { - JSONObject langObj = langs.getJSONObject( ii ); - String langCode = langObj.getString( "lc" ); - if ( ! langCode.equals( m_lc ) ) { - continue; - } - // we have our language; look for one marked default; - // otherwise take the largest. - m_langName = langObj.getString( "lang" ); - JSONArray dicts = langObj.getJSONArray( "dicts" ); - int nDicts = dicts.length(); - int theOneNWords = 0; - for ( int jj = 0; jj < nDicts; ++jj ) { - JSONObject dict = dicts.getJSONObject( jj ); - if ( dict.optBoolean( "isDflt", false ) ) { - theOne = dict; - break; - } else { - int nWords = dict.getInt( "nWords" ); - if ( null == theOne || nWords > theOneNWords ) { + if ( null != json ) { + try { + JSONObject obj = new JSONObject( json ); + JSONArray langs = obj.optJSONArray( "langs" ); + int nLangs = langs.length(); + for ( int ii = 0; ii < nLangs; ++ii ) { + JSONObject langObj = langs.getJSONObject( ii ); + String langCode = langObj.getString( "lc" ); + if ( ! langCode.equals( m_lc ) ) { + continue; + } + // we have our language; look for one marked default; + // otherwise take the largest. + m_langName = langObj.getString( "lang" ); + JSONArray dicts = langObj.getJSONArray( "dicts" ); + int nDicts = dicts.length(); + int theOneNWords = 0; + for ( int jj = 0; jj < nDicts; ++jj ) { + JSONObject dict = dicts.getJSONObject( jj ); + if ( dict.optBoolean( "isDflt", false ) ) { theOne = dict; - theOneNWords = nWords; + break; + } else { + int nWords = dict.getInt( "nWords" ); + if ( null == theOne + || nWords > theOneNWords ) { + theOne = dict; + theOneNWords = nWords; + } } } } - } - // If we got here and theOne isn't set, there is - // no wordlist available for this language. Set - // the flag so we don't try again, even though - // we've failed. - if ( null == theOne ) { - XWPrefs.setPrefsBoolean( m_context, - R.string.key_got_langdict, - true ); - } + // If we got here and theOne isn't set, there is + // no wordlist available for this language. Set + // the flag so we don't try again, even though + // we've failed. + if ( null == theOne ) { + XWPrefs.setPrefsBoolean( m_context, + R.string.key_got_langdict, + true ); + } - } catch ( JSONException ex ) { - DbgUtils.loge( ex ); - theOne = null; + } catch ( JSONException ex ) { + DbgUtils.loge( ex ); + theOne = null; + } } if ( null != theOne ) { 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 414175340..c8eb2fcc3 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/NetUtils.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/NetUtils.java @@ -234,6 +234,8 @@ public class NetUtils { } } catch( java.io.UnsupportedEncodingException uee ) { DbgUtils.loge( uee ); + } catch( java.net.UnknownHostException uhe ) { + DbgUtils.loge( uhe ); } catch( java.io.IOException ioe ) { DbgUtils.loge( ioe ); }