android下httpd 代理设置和webview代理设置
在一些android上上设置httpd代理比较简单,可以通过wlan下长按对应的wifi链接点,出现修改网络配置,然后选择显示高级选项,在代理设置里面选择手动,然后出现界面可以设置代理服务器ip和端口。
网络有些说可以通过下面方式来设置,但这个毕竟面向开发人员
1
、adb连接上target
adb kill-server
adb connect target-ip
adb shell
2
、切换到数据库文件目录
cd /data/data/com.android.providers.settings/databases
3
、使用 sqlite3 连接这个数据文件
sqlite3 settings.db
4
、利用Insert语句往system表中插入proxy的设置
ex: INSERT INTO system VALUES(_id,
'http_proxy'
,
'IPAddress:port'
);
INSERT INTO system VALUES(
2012
,
'http_proxy'
,
'192.168.10.245:3128'
);
5
、确认修改,使用
'SELECT * FROM system'
来查看一下我们插入的配置
SELECT * FROM system;
但是一些android手机不支持,而且如果我们想通过代码中webview来强制设置代理,以便我们在开发环境可以通过绑定来在手机做调试。在android1.6之前的api中有有直接的方法来对webview设置代码,但是在之后的API中去掉了。所以只能寻求别的方法来设置代码.
下面通过webview的反射来实现
import
java.lang.reflect.Field;
import
java.lang.reflect.Method;
import
org.apache.http.HttpHost;
import
android.content.Context;
import
android.util.Log;
public
class
ProxySetting {
public
static
boolean
setProxy(Context ctx, String host,
int
port) {
boolean
ret =
false
;
try
{
Log.d(
"WebViewProxySettings"
,
"setProxy defaulthost="
+ host
+
"port="
+ port);
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
;
}
}
catch
(Exception e) {
e.printStackTrace();
}
return
ret;
}
public
static
String getProxyHostname(Context ctx) {
String res =
null
;
try
{
Object requestQueueObject = getRequestQueue(ctx);
if
(requestQueueObject !=
null
) {
Object fild = getDeclaredField(requestQueueObject,
"mProxyHost"
);
if
(fild !=
null
) {
HttpHost host = (HttpHost) fild;
res = host.getHostName();
}
}
}
catch
(Exception e) {
e.printStackTrace();
}
return
res;
}
public
static
void
cancelProxy(Context ctx) {
try
{
Object requestQueueObject = getRequestQueue(ctx);
if
(requestQueueObject !=
null
) {
setDeclaredField(requestQueueObject,
"mProxyHost"
,
null
);
}
}
catch
(Exception e) {
e.printStackTrace();
}
}
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 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;
}
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);
}
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;
}
}
然后在我们通过webview加载网页的activity设置如下,在oncreate方法里面通过 ProxySetting.setProxy(this.getApplicationContext(), "30.125.11.145", 7100);设置代理服务器为30.125.11.145,端口为7100,然后我们在加载loadurl(wv,"http://30.125.11.145:7100/mbrand/brand2.htm"); http://30.125.11.145:7100/mbrand/brand2.htm 这个页面后,这个页面的相关styel,image的请求都会转发到30.125.11.145,端口为7100这个代理服务器上,然后再这个代理服务器再做转发,即可实现手机端的测试环境样式和应用绑定都可以用的问题。
import
android.app.Activity;
import
android.app.AlertDialog;
import
android.app.ProgressDialog;
import
android.content.DialogInterface;
import
android.os.Bundle;
import
android.os.Handler;
import
android.os.Message;
import
android.view.KeyEvent;
import
android.webkit.WebChromeClient;
import
android.webkit.WebView;
import
android.webkit.WebViewClient;
public
class
SpuSearchActivity
extends
Activity {
/** Called when the activity is first created. */
WebView wv;
ProgressDialog pd;
Handler handler;
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_spu_search);
init();
//执行初始化函数
handler=
new
Handler(){
public
void
handleMessage(Message msg)
{
//定义一个Handler,用于处理下载线程与UI间通讯
if
(!Thread.currentThread().isInterrupted())
{
switch
(msg.what)
{
case
0
:
pd.show();
//显示进度对话框
break
;
case
1
:
pd.hide();
//隐藏进度对话框,不可使用dismiss()、cancel(),否则再次调用show()时,显示的对话框小圆圈不会动。
break
;
}
}
super
.handleMessage(msg);
}
};
loadurl(wv,
"http://30.125.11.145:7100/mbrand/brand2.htm"
);
ProxySetting.setProxy(
this
.getApplicationContext(),
"30.125.11.145"
,
7100
);
// loadurl(wv,"http://10.16.38.43:7001/mbrand/index.htm");
// loadurl(wv,"http://bmall.1688.com/tools/mobileapp/spuSearch.htm");
// loadurl(wv,"http://192.168.1.5/app.html");
// loadurl(wv,"http://www.javaarch.net/appframework/index.html");
// loadurl(wv,"http://www.javaarch.net/demo.html");
// loadurl(wv,"http://www.gafish.net/api/demo/pull-to-refresh/index.html");
}
public
void
init(){
//初始化
wv=(WebView)findViewById(R.id.wv);
wv.getSettings().setJavaScriptEnabled(
true
);
//可用JS
wv.getSettings().setLoadWithOverviewMode(
true
);
wv.getSettings().setUseWideViewPort(
true
);
wv.setScrollBarStyle(
0
);
//滚动条风格,为0就是不给滚动条留空间,滚动条覆盖在网页上
wv.setWebViewClient(
new
WebViewClient(){
public
boolean
shouldOverrideUrlLoading(
final
WebView view,
final
String url) {
loadurl(view,url);
//载入网页
return
true
;
}
//重写点击动作,用webview载入
});
wv.setWebChromeClient(
new
WebChromeClient(){
public
void
onProgressChanged(WebView view,
int
progress){
//载入进度改变而触发
if
(progress>=
20
){
handler.sendEmptyMessage(
1
);
//如果全部载入,隐藏进度对话框
}
super
.onProgressChanged(view, progress);
}
});
pd=
new
ProgressDialog(SpuSearchActivity.
this
);
pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pd.setMessage(
"数据载入中,请稍候!"
);
}
public
boolean
onKeyDown(
int
keyCode, KeyEvent event) {
//捕捉返回键
if
((keyCode == KeyEvent.KEYCODE_BACK) && wv.canGoBack()) {
wv.goBack();
return
true
;
}
else
if
(keyCode == KeyEvent.KEYCODE_BACK){
ConfirmExit();
//按了返回键,但已经不能返回,则执行退出确认
return
true
;
}
return
super
.onKeyDown(keyCode, event);
}
public
void
ConfirmExit(){
//退出确认
AlertDialog.Builder ad=
new
AlertDialog.Builder(SpuSearchActivity.
this
);
ad.setTitle(
"退出"
);
ad.setMessage(
"是否退出应用?"
);
ad.setPositiveButton(
"是"
,
new
DialogInterface.OnClickListener() {
//退出按钮
@Override
public
void
onClick(DialogInterface dialog,
int
i) {
// TODO Auto-generated method stub
SpuSearchActivity.
this
.finish();
//关闭activity
}
});
ad.setNegativeButton(
"否"
,
new
DialogInterface.OnClickListener() {
@Override
public
void
onClick(DialogInterface dialog,
int
i) {
//不退出不用执行任何操作
}
});
ad.show();
//显示对话框
}
public
void
loadurl(
final
WebView view,
final
String url){
// new Thread(){
// public void run(){
// handler.sendEmptyMessage(0);
// view.loadUrl(url);//载入网页
// }
// }.start();
handler.sendEmptyMessage(
0
);
view.loadUrl(url);
//载入网页
}
}