mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2025-01-23 07:27:22 +01:00
remove org.apache.http (as SDK 23 prefers)
This commit is contained in:
parent
87c6b3e0fe
commit
57ba6ab693
5 changed files with 107 additions and 57 deletions
|
@ -46,7 +46,7 @@ import android.widget.LinearLayout;
|
||||||
import android.widget.ListView;
|
import android.widget.ListView;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
import org.apache.http.client.methods.HttpPost;
|
import java.net.HttpURLConnection;
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
@ -1134,11 +1134,11 @@ public class DictsDelegate extends ListDelegateBase
|
||||||
// parse less data
|
// parse less data
|
||||||
String name = null;
|
String name = null;
|
||||||
String proc = String.format( "listDicts?lc=%s", m_lc );
|
String proc = String.format( "listDicts?lc=%s", m_lc );
|
||||||
HttpPost post = NetUtils.makePost( m_context, proc );
|
HttpURLConnection conn = NetUtils.makeHttpConn( m_context, proc );
|
||||||
if ( null != post ) {
|
if ( null != conn ) {
|
||||||
JSONObject theOne = null;
|
JSONObject theOne = null;
|
||||||
String langName = null;
|
String langName = null;
|
||||||
String json = NetUtils.runPost( post, new JSONObject() );
|
String json = NetUtils.runConn( conn, new JSONObject() );
|
||||||
if ( null != json ) {
|
if ( null != json ) {
|
||||||
try {
|
try {
|
||||||
JSONObject obj = new JSONObject( json );
|
JSONObject obj = new JSONObject( json );
|
||||||
|
@ -1216,9 +1216,9 @@ public class DictsDelegate extends ListDelegateBase
|
||||||
public Boolean doInBackground( Void... unused )
|
public Boolean doInBackground( Void... unused )
|
||||||
{
|
{
|
||||||
boolean success = false;
|
boolean success = false;
|
||||||
HttpPost post = NetUtils.makePost( m_context, "listDicts" );
|
HttpURLConnection conn = NetUtils.makeHttpConn( m_context, "listDicts" );
|
||||||
if ( null != post ) {
|
if ( null != conn ) {
|
||||||
String json = NetUtils.runPost( post, new JSONObject() );
|
String json = NetUtils.runConn( conn, new JSONObject() );
|
||||||
if ( !isCancelled() ) {
|
if ( !isCancelled() ) {
|
||||||
if ( null != json ) {
|
if ( null != json ) {
|
||||||
post( new Runnable() {
|
post( new Runnable() {
|
||||||
|
|
|
@ -24,10 +24,6 @@ import android.app.AlertDialog;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.webkit.WebView;
|
import android.webkit.WebView;
|
||||||
import android.webkit.WebViewClient;
|
import android.webkit.WebViewClient;
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.io.Reader;
|
|
||||||
|
|
||||||
import org.eehouse.android.xw4.loc.LocUtils;
|
import org.eehouse.android.xw4.loc.LocUtils;
|
||||||
|
|
||||||
|
|
|
@ -22,25 +22,30 @@ package org.eehouse.android.xw4;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
|
||||||
|
import android.text.TextUtils;
|
||||||
|
import java.io.BufferedInputStream;
|
||||||
|
import java.io.BufferedWriter;
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.DataInputStream;
|
import java.io.DataInputStream;
|
||||||
import java.io.DataOutputStream;
|
import java.io.DataOutputStream;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.io.OutputStreamWriter;
|
||||||
|
import java.net.HttpURLConnection;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.net.URLEncoder;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import javax.net.SocketFactory;
|
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;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
import junit.framework.Assert;
|
||||||
|
|
||||||
public class NetUtils {
|
public class NetUtils {
|
||||||
|
|
||||||
public static final String k_PARAMS = "params";
|
public static final String k_PARAMS = "params";
|
||||||
|
@ -208,47 +213,94 @@ public class NetUtils {
|
||||||
return host;
|
return host;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static HttpPost makePost( Context context, String proc )
|
protected static HttpURLConnection makeHttpConn( Context context,
|
||||||
|
String proc )
|
||||||
{
|
{
|
||||||
|
HttpURLConnection result = null;
|
||||||
|
try {
|
||||||
String url = String.format( "%s/%s",
|
String url = String.format( "%s/%s",
|
||||||
XWPrefs.getDefaultUpdateUrl( context ),
|
XWPrefs.getDefaultUpdateUrl( context ),
|
||||||
proc );
|
proc );
|
||||||
HttpPost result;
|
result = (HttpURLConnection)new URL(url).openConnection();
|
||||||
try {
|
} catch ( java.net.MalformedURLException mue ) {
|
||||||
result = new HttpPost( url );
|
Assert.assertNull( result );
|
||||||
} catch ( IllegalArgumentException iae ) {
|
DbgUtils.loge( mue );
|
||||||
DbgUtils.loge( iae );
|
} catch ( java.io.IOException ioe ) {
|
||||||
result = null;
|
Assert.assertNull( result );
|
||||||
|
DbgUtils.loge( ioe );
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static String runPost( HttpPost post, JSONObject param )
|
protected static String runConn( HttpURLConnection conn, JSONObject param )
|
||||||
{
|
{
|
||||||
String result = null;
|
String result = null;
|
||||||
try {
|
Map<String, String> params = new HashMap<String, String>();
|
||||||
String jsonStr = param.toString();
|
params.put( k_PARAMS, param.toString() );
|
||||||
List<NameValuePair> nvp = new ArrayList<NameValuePair>();
|
String paramsString = getPostDataString( params );
|
||||||
nvp.add( new BasicNameValuePair( k_PARAMS, jsonStr ) );
|
|
||||||
post.setEntity( new UrlEncodedFormEntity(nvp) );
|
|
||||||
|
|
||||||
// Execute HTTP Post Request
|
if ( null != paramsString ) {
|
||||||
HttpClient httpclient = new DefaultHttpClient();
|
try {
|
||||||
HttpResponse response = httpclient.execute(post);
|
conn.setReadTimeout( 15000 );
|
||||||
HttpEntity entity = response.getEntity();
|
conn.setConnectTimeout( 15000 );
|
||||||
if ( null != entity ) {
|
conn.setRequestMethod( "POST" );
|
||||||
result = EntityUtils.toString( entity );
|
conn.setDoInput( true );
|
||||||
if ( 0 == result.length() ) {
|
conn.setDoOutput( true );
|
||||||
result = null;
|
conn.setFixedLengthStreamingMode( paramsString.length() );
|
||||||
|
|
||||||
|
OutputStream os = conn.getOutputStream();
|
||||||
|
BufferedWriter writer
|
||||||
|
= new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
|
||||||
|
writer.write( paramsString );
|
||||||
|
writer.flush();
|
||||||
|
writer.close();
|
||||||
|
os.close();
|
||||||
|
|
||||||
|
int responseCode = conn.getResponseCode();
|
||||||
|
if ( HttpURLConnection.HTTP_OK == responseCode ) {
|
||||||
|
InputStream is = conn.getInputStream();
|
||||||
|
BufferedInputStream bis = new BufferedInputStream( is );
|
||||||
|
|
||||||
|
ByteArrayOutputStream bas = new ByteArrayOutputStream();
|
||||||
|
byte[] buffer = new byte[1024];
|
||||||
|
for ( ; ; ) {
|
||||||
|
int nRead = bis.read( buffer );
|
||||||
|
if ( 0 > nRead ) {
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
bas.write( buffer, 0, nRead );
|
||||||
}
|
}
|
||||||
} catch( java.io.UnsupportedEncodingException uee ) {
|
result = new String( bas.toByteArray() );
|
||||||
DbgUtils.loge( uee );
|
} else {
|
||||||
} catch( java.net.UnknownHostException uhe ) {
|
DbgUtils.logf( "runConn: responseCode: %d", responseCode );
|
||||||
DbgUtils.loge( uhe );
|
}
|
||||||
|
} catch ( java.net.ProtocolException pe ) {
|
||||||
|
DbgUtils.loge( pe );
|
||||||
} catch( java.io.IOException ioe ) {
|
} catch( java.io.IOException ioe ) {
|
||||||
DbgUtils.loge( ioe );
|
DbgUtils.loge( ioe );
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String getPostDataString( Map<String, String> params )
|
||||||
|
{
|
||||||
|
String result = null;
|
||||||
|
try {
|
||||||
|
ArrayList<String> pairs = new ArrayList<String>();
|
||||||
|
// StringBuilder sb = new StringBuilder();
|
||||||
|
String[] pair = { null, null };
|
||||||
|
for ( Map.Entry<String, String> entry : params.entrySet() ){
|
||||||
|
pair[0] = URLEncoder.encode( entry.getKey(), "UTF-8" );
|
||||||
|
pair[1] = URLEncoder.encode( entry.getValue(), "UTF-8" );
|
||||||
|
pairs.add( TextUtils.join( "=", pair ) );
|
||||||
|
}
|
||||||
|
result = TextUtils.join( "&", pairs );
|
||||||
|
} catch ( java.io.UnsupportedEncodingException uee ) {
|
||||||
|
DbgUtils.loge( uee );
|
||||||
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -41,6 +41,8 @@ import android.widget.ArrayAdapter;
|
||||||
import android.widget.CompoundButton;
|
import android.widget.CompoundButton;
|
||||||
import android.widget.ImageButton;
|
import android.widget.ImageButton;
|
||||||
import android.widget.Spinner;
|
import android.widget.Spinner;
|
||||||
|
|
||||||
|
import java.net.HttpURLConnection;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
|
@ -49,7 +51,7 @@ import java.util.HashSet;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import org.apache.http.client.methods.HttpPost;
|
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
@ -512,9 +514,9 @@ public class RelayInviteDelegate extends InviteDelegate {
|
||||||
params.put( "me", DevID.getRelayDevIDInt( m_activity ) );
|
params.put( "me", DevID.getRelayDevIDInt( m_activity ) );
|
||||||
DbgUtils.logf( "sending to server: %s", params.toString() );
|
DbgUtils.logf( "sending to server: %s", params.toString() );
|
||||||
|
|
||||||
HttpPost post = NetUtils.makePost( m_context, "opponentIDsFor" );
|
HttpURLConnection conn = NetUtils.makeHttpConn( m_context, "opponentIDsFor" );
|
||||||
if ( null != post ) {
|
if ( null != conn ) {
|
||||||
String str = NetUtils.runPost( post, params );
|
String str = NetUtils.runConn( conn, params );
|
||||||
DbgUtils.logf( "got json from server: %s", str );
|
DbgUtils.logf( "got json from server: %s", str );
|
||||||
reply = new JSONObject( str );
|
reply = new JSONObject( str );
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,12 +30,12 @@ import android.content.pm.PackageManager;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.AsyncTask;
|
import android.os.AsyncTask;
|
||||||
import android.os.SystemClock;
|
import android.os.SystemClock;
|
||||||
|
import java.net.HttpURLConnection;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.apache.http.client.methods.HttpPost;
|
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
@ -260,10 +260,10 @@ public class UpdateCheckReceiver extends BroadcastReceiver {
|
||||||
|
|
||||||
@Override protected String doInBackground( Void... unused )
|
@Override protected String doInBackground( Void... unused )
|
||||||
{
|
{
|
||||||
HttpPost post = NetUtils.makePost( m_context, "getUpdates" );
|
HttpURLConnection conn = NetUtils.makeHttpConn( m_context, "getUpdates" );
|
||||||
String json = null;
|
String json = null;
|
||||||
if ( null != post ) {
|
if ( null != conn ) {
|
||||||
json = NetUtils.runPost( post, m_params );
|
json = NetUtils.runConn( conn, m_params );
|
||||||
}
|
}
|
||||||
return json;
|
return json;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue