4

My Android App is running it's own webserver on port 24783.

I can connect to it when I'm in my local wireless. I enter the local wifi ip and the port in the phones chrome browser and the website appears. However, when I'm offline (but the server is running locally on my phone), I can't connect to 127.0.0.1:24783 and additionally when I'm only connected over 4G I can not connect to 127.0.0.1:24783 either because all traffic goes over my carriers proxy and wont allow me to connect on 127.0.0.1

Is there a way to tell the phone that for 127.0.0.1 it should not talk to the proxy? That 127.0.0.1 is always "local"? I'm happy to use my own webview.

UPDATE:

Turns out this is only a KitKat issue.

I haven't found a solution but go a bit closer.

The solution bellow only works when I open the app, close it using the back button and reopen it again:

package com.ndream.console;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.net.Proxy;
import android.os.Build;
import android.os.Parcelable;
import android.util.ArrayMap;
import android.util.Log;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created by andrin on 15/01/15.
 */
public class ProxyUtils {
    @TargetApi(Build.VERSION_CODES.KITKAT)
    public static void setKitKatWebViewProxy(Context appContext, String host, int port) {
        if (Build.VERSION.SDK_INT != 19) {
            return;
        }
        Log.i("Console", "PROXY MANUALLY SET");
        System.setProperty("http.proxyHost", host);
        System.setProperty("http.proxyPort", port + "");
        System.setProperty("https.proxyHost", host);
        System.setProperty("https.proxyPort", port + "");
        try {
            Class applictionCls = Class.forName("android.app.Application");
            Field loadedApkField = applictionCls.getDeclaredField("mLoadedApk");
            loadedApkField.setAccessible(true);
            Object loadedApk = loadedApkField.get(appContext);
            Class loadedApkCls = Class.forName("android.app.LoadedApk");
            Field receiversField = loadedApkCls.getDeclaredField("mReceivers");
            receiversField.setAccessible(true);
            ArrayMap receivers = (ArrayMap) receiversField.get(loadedApk);
            for (Object receiverMap : receivers.values()) {
                for (Object rec : ((ArrayMap) receiverMap).keySet()) {
                    Class clazz = rec.getClass();
                    if (clazz.getName().contains("ProxyChangeListener")) {
                        Method onReceiveMethod = clazz.getDeclaredMethod("onReceive", Context.class, Intent.class);
                        Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);

                        /*********** optional, may be need in future *************/
                        final String CLASS_NAME = "android.net.ProxyProperties";
                        Class cls = Class.forName(CLASS_NAME);
                        Constructor constructor = cls.getConstructor(String.class, Integer.TYPE, String.class);
                        constructor.setAccessible(true);
                        Object proxyProperties = constructor.newInstance(host, port, null);
                        intent.putExtra("proxy", (Parcelable) proxyProperties);
                        /*********** optional, may be need in future *************/

                        onReceiveMethod.invoke(rec, appContext, intent);
                    }
                }
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
    }
}

and then on startup I call:

ProxyUtils.setKitKatWebViewProxy(getApplicationContext(), "", 0);

0 Answers0