Bonjour,

je souhaite pouvoir injecté du code JS dans une page HTML (qui comporte des classes qui me permettront des faire des appels ultérieurement dans le programme).

Je dois donc mettre en place un proxy, récupérer le code de la page, y injecter mon code JS et l'envoyer vers une webwiew.

Je n'arrive pas bien mettre en place un proxy sur ma tablette (ICS 4.0) pour récupérer le code.

J'ai récupérer un code sur le net mais il ne semble pas fonctionner

J'appelle la fonction setProxy("127.0.0.1",8080) de la classe suivante
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
 
package com.android.testproxy;
 
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
 
import org.apache.http.HttpHost;
 
import android.content.Context;
import android.os.Build;
import android.util.Log;
 
/**
 * Utility class for setting WebKit proxy used by Android WebView
 *
 */
public class ProxySettings {
 
	private static final String TAG = "GAEProxy.ProxySettings";
 
	static final int PROXY_CHANGED = 193;
 
	private static Object getDeclaredField(Object obj, String name) throws SecurityException,
	NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
		Field f = obj.getClass().getDeclaredField(name);
		f.setAccessible(true);
		Object out = f.get(obj);
		// System.out.println(obj.getClass().getName() + "." + name + " = "+
		// out);
		return out;
	}
 
	public static Object getRequestQueue(Context ctx) throws Exception {
		Object ret = null;
		Class networkClass = Class.forName("android.webkit.Network");
		if (networkClass != null) {
			Object networkObj = invokeMethod(networkClass, "getInstance", new Object[] { ctx },
					Context.class);
			if (networkObj != null) {
				ret = getDeclaredField(networkObj, "mRequestQueue");
			}
		}
		return ret;
	}
 
	private static Object invokeMethod(Object object, String methodName, Object[] params,
			Class... types) throws Exception {
		Object out = null;
		Class c = object instanceof Class ? (Class) object : object.getClass();
		if (types != null) {
			Method method = c.getMethod(methodName, types);
			out = method.invoke(object, params);
		} else {
			Method method = c.getMethod(methodName);
			out = method.invoke(object);
		}
		// System.out.println(object.getClass().getName() + "." + methodName +
		// "() = "+ out);
		return out;
	}
 
	public static void resetProxy(Context ctx) throws Exception {
		Object requestQueueObject = getRequestQueue(ctx);
		if (requestQueueObject != null) {
			setDeclaredField(requestQueueObject, "mProxyHost", null);
		}
	}
 
	private static void setDeclaredField(Object obj, String name, Object value)
			throws SecurityException, NoSuchFieldException, IllegalArgumentException,
			IllegalAccessException {
		Field f = obj.getClass().getDeclaredField(name);
		f.setAccessible(true);
		f.set(obj, value);
	}
 
	/**
         * Override WebKit Proxy settings
         *
         * @param ctx
         * Android ApplicationContext
         * @param host
         * @param port
         * @return true if Proxy was successfully set
         */
	public static boolean setProxy(Context ctx, String host, int port) {
		boolean ret = false;
		setSystemProperties(host, port);
 
		try {
			if (Build.VERSION.SDK_INT < 14) {
 
				Object requestQueueObject = getRequestQueue(ctx);
				if (requestQueueObject != null) {
					// Create Proxy config object and set it into request Q
					HttpHost httpHost = new HttpHost(host, port, "http");
 
					setDeclaredField(requestQueueObject, "mProxyHost", httpHost);
					ret = true;
				}
 
			} else {
				ret = setICSProxy(host, port);
			}
		} catch (Exception e) {
			Log.e(TAG, "error setting up webkit proxying", e);
		}
		return ret;
	}
 
	private static boolean setICSProxy(String host, int port) throws ClassNotFoundException,
	NoSuchMethodException, IllegalArgumentException, InstantiationException,
	IllegalAccessException, InvocationTargetException {
		Class webViewCoreClass = Class.forName("android.webkit.WebViewCore");
		Class proxyPropertiesClass = Class.forName("android.net.ProxyProperties");
		if (webViewCoreClass != null && proxyPropertiesClass != null) {
			Method m = webViewCoreClass.getDeclaredMethod("sendStaticMessage", Integer.TYPE,
					Object.class);
			Constructor c = proxyPropertiesClass.getConstructor(String.class, Integer.TYPE,
					String.class);
			m.setAccessible(true);
			c.setAccessible(true);
			Object properties = c.newInstance(host, port, null);
			m.invoke(null, PROXY_CHANGED, properties);
			return true;
		}
		return false;
 
	}
 
	private static void setSystemProperties(String host, int port) {
 
		System.setProperty("http.proxyHost", host);
		System.setProperty("http.proxyPort", port + "");
 
		System.setProperty("https.proxyHost", host);
		System.setProperty("https.proxyPort", port + "");
 
	}
}
Ensuite je configure ma webview pour qu'elle se connecte via le proxy
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
 
public void ConfigureWebviewProxy(){
		try
		{
		  Class jwcjb = Class.forName("android.webkit.JWebCoreJavaBridge");
		  Class params[] = new Class[1];
		  params[0] = Class.forName("android.net.ProxyProperties");
		  Method updateProxyInstance = jwcjb.getDeclaredMethod("updateProxy", params);
 
		  Class wv = Class.forName("android.webkit.WebView");
		  Field mWebViewCoreField = wv.getDeclaredField("mWebViewCore");
		  Object mWebViewCoreFieldIntance = getFieldValueSafely(mWebViewCoreField, mWebView);
 
		  Class wvc = Class.forName("android.webkit.WebViewCore");
		  Field mBrowserFrameField = wvc.getDeclaredField("mBrowserFrame");
		  Object mBrowserFrame = getFieldValueSafely(mBrowserFrameField, mWebViewCoreFieldIntance);
 
		  Class bf = Class.forName("android.webkit.BrowserFrame");
		  Field sJavaBridgeField = bf.getDeclaredField("sJavaBridge");
		  Object sJavaBridge = getFieldValueSafely(sJavaBridgeField, mBrowserFrame);
 
		  Class ppclass = Class.forName("android.net.ProxyProperties");
		  Class pparams[] = new Class[3];
		  pparams[0] = String.class;
		  pparams[1] = int.class;
		  pparams[2] = String.class;
		  Constructor ppcont = ppclass.getConstructor(pparams);
 
		  updateProxyInstance.invoke(sJavaBridge, ppcont.newInstance("127.0.0.1", 8080, null)); 
		}
		catch (Exception ex)
		{ 
			ex.printStackTrace();
			Log.e("Webview proxy", "Error");
		}
 
	}
et lorsque je fais webview.loadURL("http://www.google.fr"), rien ne se passe.
Si quelqu'un a une idée,

Merci d'avance !