From d426db7a4958b878bc87baccff38daff9c5d96b3 Mon Sep 17 00:00:00 2001 From: Eric House Date: Fri, 26 Jun 2015 06:37:34 -0700 Subject: [PATCH] move httppost stuff into NetUtils --- .../eehouse/android/xw4/DictsDelegate.java | 8 +-- .../src/org/eehouse/android/xw4/NetUtils.java | 57 +++++++++++++++++++ .../android/xw4/UpdateCheckReceiver.java | 57 +------------------ 3 files changed, 64 insertions(+), 58 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 3f52c0e91..9fdc0e99d 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictsDelegate.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictsDelegate.java @@ -1135,11 +1135,11 @@ public class DictsDelegate extends ListDelegateBase // parse less data String name = null; String proc = String.format( "listDicts?lc=%s", m_lc ); - HttpPost post = UpdateCheckReceiver.makePost( m_context, proc ); + HttpPost post = NetUtils.makePost( m_context, proc ); if ( null != post ) { JSONObject theOne = null; String langName = null; - String json = UpdateCheckReceiver.runPost( post, new JSONObject() ); + String json = NetUtils.runPost( post, new JSONObject() ); try { JSONObject obj = new JSONObject( json ); JSONArray langs = obj.optJSONArray( "langs" ); @@ -1214,9 +1214,9 @@ public class DictsDelegate extends ListDelegateBase public Boolean doInBackground( Void... unused ) { boolean success = false; - HttpPost post = UpdateCheckReceiver.makePost( m_context, "listDicts" ); + HttpPost post = NetUtils.makePost( m_context, "listDicts" ); if ( null != post ) { - String json = UpdateCheckReceiver.runPost( post, new JSONObject() ); + String json = NetUtils.runPost( post, new JSONObject() ); if ( !isCancelled() ) { if ( null != json ) { post( new Runnable() { 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 beab8b46e..414175340 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/NetUtils.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/NetUtils.java @@ -21,14 +21,29 @@ package org.eehouse.android.xw4; import android.content.Context; + import java.io.DataInputStream; import java.io.DataOutputStream; import java.net.InetAddress; import java.net.Socket; +import java.util.ArrayList; +import java.util.List; import javax.net.SocketFactory; +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.NameValuePair; +import org.apache.http.client.HttpClient; +import org.apache.http.client.entity.UrlEncodedFormEntity; +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; + public class NetUtils { + public static final String k_PARAMS = "params"; public static final byte PROTOCOL_VERSION = 0; // from xwrelay.h public static byte PRX_PUB_ROOMS = 1; @@ -183,6 +198,48 @@ public class NetUtils { return msgs; } // queryRelay + protected static HttpPost makePost( Context context, String proc ) + { + String url = String.format( "%s/%s", + XWPrefs.getDefaultUpdateUrl( context ), + proc ); + HttpPost result; + try { + result = new HttpPost( url ); + } catch ( IllegalArgumentException iae ) { + DbgUtils.loge( iae ); + result = null; + } + return result; + } + + protected static String runPost( HttpPost post, JSONObject param ) + { + String result = null; + try { + String jsonStr = param.toString(); + List nvp = new ArrayList(); + nvp.add( new BasicNameValuePair( k_PARAMS, jsonStr ) ); + post.setEntity( new UrlEncodedFormEntity(nvp) ); + + // Execute HTTP Post Request + HttpClient httpclient = new DefaultHttpClient(); + HttpResponse response = httpclient.execute(post); + HttpEntity entity = response.getEntity(); + if ( null != entity ) { + result = EntityUtils.toString( entity ); + if ( 0 == result.length() ) { + result = null; + } + } + } catch( java.io.UnsupportedEncodingException uee ) { + DbgUtils.loge( uee ); + } catch( java.io.IOException ioe ) { + DbgUtils.loge( ioe ); + } + return result; + } + private static int sumStrings( final String[] strs ) { int len = 0; diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/UpdateCheckReceiver.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/UpdateCheckReceiver.java index 6d2106d54..9234b5954 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/UpdateCheckReceiver.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/UpdateCheckReceiver.java @@ -30,20 +30,12 @@ import android.content.pm.PackageManager; import android.net.Uri; import android.os.AsyncTask; import android.os.SystemClock; + import java.io.File; import java.util.ArrayList; import java.util.List; -import org.apache.http.HttpEntity; -import org.apache.http.HttpResponse; -import org.apache.http.NameValuePair; -import org.apache.http.client.ClientProtocolException; -import org.apache.http.client.HttpClient; -import org.apache.http.client.entity.UrlEncodedFormEntity; 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.JSONArray; import org.json.JSONObject; @@ -73,7 +65,6 @@ public class UpdateCheckReceiver extends BroadcastReceiver { private static final String k_MD5SUM = "md5sum"; private static final String k_INDEX = "index"; private static final String k_URL = "url"; - private static final String k_PARAMS = "params"; private static final String k_DEVID = "did"; private static final String k_DEBUG = "dbg"; private static final String k_XLATEINFO = "xlatinfo"; @@ -221,48 +212,6 @@ public class UpdateCheckReceiver extends BroadcastReceiver { return result; } - protected static HttpPost makePost( Context context, String proc ) - { - String url = String.format( "%s/%s", - XWPrefs.getDefaultUpdateUrl( context ), - proc ); - HttpPost result; - try { - result = new HttpPost( url ); - } catch ( IllegalArgumentException iae ) { - DbgUtils.loge( iae ); - result = null; - } - return result; - } - - protected static String runPost( HttpPost post, JSONObject params ) - { - String result = null; - try { - String jsonStr = params.toString(); - List nvp = new ArrayList(); - nvp.add( new BasicNameValuePair( k_PARAMS, jsonStr ) ); - post.setEntity( new UrlEncodedFormEntity(nvp) ); - - // Execute HTTP Post Request - HttpClient httpclient = new DefaultHttpClient(); - HttpResponse response = httpclient.execute(post); - HttpEntity entity = response.getEntity(); - if ( null != entity ) { - result = EntityUtils.toString( entity ); - if ( 0 == result.length() ) { - result = null; - } - } - } catch( java.io.UnsupportedEncodingException uee ) { - DbgUtils.loge( uee ); - } catch( java.io.IOException ioe ) { - DbgUtils.loge( ioe ); - } - return result; - } - private static JSONObject makeDictParams( Context context, DictUtils.DictAndLoc dal, int index ) @@ -311,10 +260,10 @@ public class UpdateCheckReceiver extends BroadcastReceiver { @Override protected String doInBackground( Void... unused ) { - HttpPost post = makePost( m_context, "getUpdates" ); + HttpPost post = NetUtils.makePost( m_context, "getUpdates" ); String json = null; if ( null != post ) { - json = runPost( post, m_params ); + json = NetUtils.runPost( post, m_params ); } return json; }