remove org.apache.http (as SDK 23 prefers)

This commit is contained in:
Eric House 2015-12-13 12:11:37 -08:00
parent 87c6b3e0fe
commit 57ba6ab693
5 changed files with 107 additions and 57 deletions

View file

@ -46,7 +46,7 @@ import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import org.apache.http.client.methods.HttpPost;
import java.net.HttpURLConnection;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
@ -1134,11 +1134,11 @@ public class DictsDelegate extends ListDelegateBase
// parse less data
String name = null;
String proc = String.format( "listDicts?lc=%s", m_lc );
HttpPost post = NetUtils.makePost( m_context, proc );
if ( null != post ) {
HttpURLConnection conn = NetUtils.makeHttpConn( m_context, proc );
if ( null != conn ) {
JSONObject theOne = null;
String langName = null;
String json = NetUtils.runPost( post, new JSONObject() );
String json = NetUtils.runConn( conn, new JSONObject() );
if ( null != json ) {
try {
JSONObject obj = new JSONObject( json );
@ -1216,9 +1216,9 @@ public class DictsDelegate extends ListDelegateBase
public Boolean doInBackground( Void... unused )
{
boolean success = false;
HttpPost post = NetUtils.makePost( m_context, "listDicts" );
if ( null != post ) {
String json = NetUtils.runPost( post, new JSONObject() );
HttpURLConnection conn = NetUtils.makeHttpConn( m_context, "listDicts" );
if ( null != conn ) {
String json = NetUtils.runConn( conn, new JSONObject() );
if ( !isCancelled() ) {
if ( null != json ) {
post( new Runnable() {

View file

@ -24,10 +24,6 @@ import android.app.AlertDialog;
import android.content.Context;
import android.webkit.WebView;
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;

View file

@ -22,25 +22,30 @@ package org.eehouse.android.xw4;
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.DataOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.Socket;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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 junit.framework.Assert;
public class NetUtils {
public static final String k_PARAMS = "params";
@ -208,47 +213,94 @@ public class NetUtils {
return host;
}
protected static HttpPost makePost( Context context, String proc )
protected static HttpURLConnection makeHttpConn( Context context,
String proc )
{
String url = String.format( "%s/%s",
XWPrefs.getDefaultUpdateUrl( context ),
proc );
HttpPost result;
HttpURLConnection result = null;
try {
result = new HttpPost( url );
} catch ( IllegalArgumentException iae ) {
DbgUtils.loge( iae );
result = null;
String url = String.format( "%s/%s",
XWPrefs.getDefaultUpdateUrl( context ),
proc );
result = (HttpURLConnection)new URL(url).openConnection();
} catch ( java.net.MalformedURLException mue ) {
Assert.assertNull( result );
DbgUtils.loge( mue );
} catch ( java.io.IOException ioe ) {
Assert.assertNull( result );
DbgUtils.loge( ioe );
}
return result;
}
protected static String runPost( HttpPost post, JSONObject param )
protected static String runConn( HttpURLConnection conn, JSONObject param )
{
String result = null;
Map<String, String> params = new HashMap<String, String>();
params.put( k_PARAMS, param.toString() );
String paramsString = getPostDataString( params );
if ( null != paramsString ) {
try {
conn.setReadTimeout( 15000 );
conn.setConnectTimeout( 15000 );
conn.setRequestMethod( "POST" );
conn.setDoInput( true );
conn.setDoOutput( true );
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 );
}
result = new String( bas.toByteArray() );
} else {
DbgUtils.logf( "runConn: responseCode: %d", responseCode );
}
} catch ( java.net.ProtocolException pe ) {
DbgUtils.loge( pe );
} catch( java.io.IOException ioe ) {
DbgUtils.loge( ioe );
}
}
return result;
}
private static String getPostDataString( Map<String, String> params )
{
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;
}
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 ) );
}
} catch( java.io.UnsupportedEncodingException uee ) {
result = TextUtils.join( "&", pairs );
} catch ( java.io.UnsupportedEncodingException uee ) {
DbgUtils.loge( uee );
} catch( java.net.UnknownHostException uhe ) {
DbgUtils.loge( uhe );
} catch( java.io.IOException ioe ) {
DbgUtils.loge( ioe );
}
return result;
}

View file

@ -41,6 +41,8 @@ import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.Spinner;
import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
@ -49,7 +51,7 @@ import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.apache.http.client.methods.HttpPost;
import org.json.JSONArray;
import org.json.JSONObject;
@ -512,9 +514,9 @@ public class RelayInviteDelegate extends InviteDelegate {
params.put( "me", DevID.getRelayDevIDInt( m_activity ) );
DbgUtils.logf( "sending to server: %s", params.toString() );
HttpPost post = NetUtils.makePost( m_context, "opponentIDsFor" );
if ( null != post ) {
String str = NetUtils.runPost( post, params );
HttpURLConnection conn = NetUtils.makeHttpConn( m_context, "opponentIDsFor" );
if ( null != conn ) {
String str = NetUtils.runConn( conn, params );
DbgUtils.logf( "got json from server: %s", str );
reply = new JSONObject( str );
}

View file

@ -30,12 +30,12 @@ import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.SystemClock;
import java.net.HttpURLConnection;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.client.methods.HttpPost;
import org.json.JSONArray;
import org.json.JSONObject;
@ -260,10 +260,10 @@ public class UpdateCheckReceiver extends BroadcastReceiver {
@Override protected String doInBackground( Void... unused )
{
HttpPost post = NetUtils.makePost( m_context, "getUpdates" );
HttpURLConnection conn = NetUtils.makeHttpConn( m_context, "getUpdates" );
String json = null;
if ( null != post ) {
json = NetUtils.runPost( post, m_params );
if ( null != conn ) {
json = NetUtils.runConn( conn, m_params );
}
return json;
}