Connect to the Android 4.4 phone (localhost) in airplane mode

My Android application runs its own web server on port 24783.

I can connect to it when I'm on my local wireless network. I enter the local wifi ip and port in Chrome Chrome browser and the website appears. However, when I am 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 to 4G, I can’t connect to 127.0.0.1:24783 either because that all traffic goes through the proxy server of my operators and does not allow me to connect to 127.0.0.1

Is there any way to tell the phone that for 127.0.0.1 it should not talk to the proxy server? What is 127.0.0.1 always "locally"? I am happy to use my own webview.

UPDATE:

Turns out this is just KitKat's problem.

I did not find a solution, but a little closer.

The solution below only works when I open the application, close it using the "Back" button, and open 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 at startup I call:

ProxyUtils.setKitKatWebViewProxy(getApplicationContext(), "", 0);
+4
source share

All Articles