mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2025-02-04 20:46:28 +01:00
move httppost stuff into NetUtils
This commit is contained in:
parent
8a575bc789
commit
d426db7a49
3 changed files with 64 additions and 58 deletions
|
@ -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() {
|
||||
|
|
|
@ -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<NameValuePair> nvp = new ArrayList<NameValuePair>();
|
||||
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;
|
||||
|
|
|
@ -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<NameValuePair> nvp = new ArrayList<NameValuePair>();
|
||||
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;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue