talk to relay.py/kill, first web api use from android

And it seems to work!
This commit is contained in:
Eric House 2017-11-11 08:22:52 -08:00
parent 31123b72ce
commit 0831bd8022
4 changed files with 80 additions and 12 deletions

View file

@ -1238,7 +1238,7 @@ 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 );
HttpURLConnection conn = NetUtils.makeHttpConn( m_context, proc ); HttpURLConnection conn = NetUtils.makeHttpUpdateConn( m_context, proc );
if ( null != conn ) { if ( null != conn ) {
JSONObject theOne = null; JSONObject theOne = null;
String langName = null; String langName = null;
@ -1320,7 +1320,7 @@ public class DictsDelegate extends ListDelegateBase
public Boolean doInBackground( Void... unused ) public Boolean doInBackground( Void... unused )
{ {
boolean success = false; boolean success = false;
HttpURLConnection conn = NetUtils.makeHttpConn( m_context, "listDicts" ); HttpURLConnection conn = NetUtils.makeHttpUpdateConn( m_context, "listDicts" );
if ( null != conn ) { if ( null != conn ) {
String json = NetUtils.runConn( conn, new JSONObject() ); String json = NetUtils.runConn( conn, new JSONObject() );
if ( !isCancelled() ) { if ( !isCancelled() ) {

View file

@ -25,6 +25,8 @@ import android.text.TextUtils;
import junit.framework.Assert; import junit.framework.Assert;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
@ -88,7 +90,41 @@ public class NetUtils {
m_obits = obits; m_obits = obits;
} }
public void run() { public void run()
{
if ( XWPrefs.getPreferWebAPI( m_context ) ) {
runViaWeb();
} else {
runWithProxySocket();
}
}
private void runViaWeb()
{
try {
JSONArray params = new JSONArray();
for ( int ii = 0; ii < m_obits.length; ++ii ) {
JSONObject one = new JSONObject();
one.put( "relayID", m_obits[ii].m_relayID );
one.put( "seed", m_obits[ii].m_seed );
params.put( one );
}
HttpURLConnection conn = makeHttpRelayConn( m_context, "kill" );
Log.d( TAG, "kill params: %s", params.toString() );
String resStr = runConn( conn, params );
Log.d( TAG, "kill => %s", resStr );
JSONObject result = new JSONObject( resStr );
if ( 0 == result.optInt( "err", -1 ) ) {
DBUtils.clearObits( m_context, m_obits );
}
} catch ( JSONException ex ) {
Assert.assertFalse( BuildConfig.DEBUG );
}
}
private void runWithProxySocket()
{
Socket socket = makeProxySocket( m_context, 10000 ); Socket socket = makeProxySocket( m_context, 10000 );
if ( null != socket ) { if ( null != socket ) {
int strLens = 0; int strLens = 0;
@ -139,8 +175,7 @@ public class NetUtils {
{ {
DBUtils.Obit[] obits = DBUtils.listObits( context ); DBUtils.Obit[] obits = DBUtils.listObits( context );
if ( null != obits && 0 < obits.length ) { if ( null != obits && 0 < obits.length ) {
InformThread thread = new InformThread( context, obits ); new InformThread( context, obits ).start();
thread.start();
} }
} }
@ -214,14 +249,26 @@ public class NetUtils {
return host; return host;
} }
protected static HttpURLConnection makeHttpConn( Context context, protected static HttpURLConnection makeHttpRelayConn( Context context,
String proc ) String proc )
{
String url = XWPrefs.getDefaultRelayUrl( context );
return makeHttpConn( context, url, proc );
}
protected static HttpURLConnection makeHttpUpdateConn( Context context,
String proc )
{
String url = XWPrefs.getDefaultUpdateUrl( context );
return makeHttpConn( context, url, proc );
}
private static HttpURLConnection makeHttpConn( Context context,
String path, String proc )
{ {
HttpURLConnection result = null; HttpURLConnection result = null;
try { try {
String url = String.format( "%s/%s", String url = String.format( "%s/%s", path, proc );
XWPrefs.getDefaultUpdateUrl( context ),
proc );
result = (HttpURLConnection)new URL(url).openConnection(); result = (HttpURLConnection)new URL(url).openConnection();
} catch ( java.net.MalformedURLException mue ) { } catch ( java.net.MalformedURLException mue ) {
Assert.assertNull( result ); Assert.assertNull( result );
@ -233,11 +280,21 @@ public class NetUtils {
return result; return result;
} }
protected static String runConn( HttpURLConnection conn, JSONArray param )
{
return runConn( conn, param.toString() );
}
protected static String runConn( HttpURLConnection conn, JSONObject param ) protected static String runConn( HttpURLConnection conn, JSONObject param )
{
return runConn( conn, param.toString() );
}
private static String runConn( HttpURLConnection conn, String param )
{ {
String result = null; String result = null;
Map<String, String> params = new HashMap<String, String>(); Map<String, String> params = new HashMap<String, String>();
params.put( k_PARAMS, param.toString() ); params.put( k_PARAMS, param );
String paramsString = getPostDataString( params ); String paramsString = getPostDataString( params );
if ( null != paramsString ) { if ( null != paramsString ) {

View file

@ -258,7 +258,8 @@ public class UpdateCheckReceiver extends BroadcastReceiver {
@Override protected String doInBackground( Void... unused ) @Override protected String doInBackground( Void... unused )
{ {
HttpURLConnection conn = NetUtils.makeHttpConn( m_context, "getUpdates" ); HttpURLConnection conn
= NetUtils.makeHttpUpdateConn( m_context, "getUpdates" );
String json = null; String json = null;
if ( null != conn ) { if ( null != conn ) {
json = NetUtils.runConn( conn, m_params ); json = NetUtils.runConn( conn, m_params );

View file

@ -115,6 +115,16 @@ public class XWPrefs {
return getPrefsString( context, R.string.key_update_url ); return getPrefsString( context, R.string.key_update_url );
} }
public static String getDefaultRelayUrl( Context context )
{
return getPrefsString( context, R.string.key_relay_url );
}
public static boolean getPreferWebAPI( Context context )
{
return getPrefsBoolean( context, R.string.key_relay_via_http, false );
}
public static int getDefaultProxyPort( Context context ) public static int getDefaultProxyPort( Context context )
{ {
String val = getPrefsString( context, R.string.key_proxy_port ); String val = getPrefsString( context, R.string.key_proxy_port );