简体   繁体   English

如何设置 Android 模拟器代理设置

[英]How to set up Android emulator proxy settings

I want to to use the browser inside the Android emulator, and I want to use the proxy settings on my machine.我想在 Android 模拟器中使用浏览器,并且我想在我的机器上使用代理设置。 How can I set this up?我该如何设置?

Reading the very good Android manuals, they tell me that I should start Android using the following command:阅读了非常好的 Android 手册,他们告诉我应该使用以下命令启动 Android:

emulator -avd myavd -http-proxy http://168.192.1.2:3300

But I am still not able to use the emulator browser.但我仍然无法使用模拟器浏览器。 Please note that I am using the IP address for my proxy server.请注意,我正在使用代理服务器的 IP 地址。

What am I doing wrong?我究竟做错了什么?

The simplest and the best way is to do the following: This has been done for Android Emulator 2.2最简单和最好的方法是执行以下操作:Android Emulator 2.2 已完成此操作

  1. Click on Menu点击菜单
  2. Click on Settings点击设置
  3. Click on Wireless & Networks点击无线和网络
  4. Go to Mobile Networks前往移动网络
  5. Go to Access Point Names转至接入点名称
  6. Here you will Telkila Internet, click on it.在这里,您将 Telkila Internet,单击它。
  7. In the Edit access point section, input the "proxy" and "port"在编辑接入点部分,输入“代理”和“端口”
  8. Also provide the Username and Password, rest of the fields leave them blank.还提供用户名和密码,其余字段留空。

-Android 模拟器上的 http-proxy

On Run Configuration> Android Application > App > Target > Additional Emulator Command Line Options: -http-proxy http://xx.xxx.xx.xx:8080在运行配置> Android应用程序>应用程序>目标>附加模拟器命令行选项:-http-proxy http://xx.xxx.xx.xx:8080

There is a setting in Android emulator to set the proxy. Android 模拟器中有一个设置来设置代理。 在此处输入图片说明

我在服务器名称中删除 http 后尝试过,它对我有用。

emulator -avd myavd -http-proxy 168.192.1.2:3300

This will not help for the browser, but you can also define a proxy in your code to use with a HTTP client:这对浏览器没有帮助,但您也可以在代码中定义一个代理以与 HTTP 客户端一起使用:

// proxy
private static final String PROXY = "123.123.123.123";
// proxy host
private static final HttpHost PROXY_HOST = new HttpHost(PROXY, 8080);
HttpParams httpParameters = new BasicHttpParams();
DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, PROXY_HOST);

On Android Studio:在 Android Studio 上:

Click on Edit Configuration under App Menu单击应用程序菜单下的编辑配置

在此处输入图片说明

  1. Go to App or Android App (as default settings)转到应用程序或 Android 应用程序(作为默认设置)
  2. tap on Debugger点击调试器
  3. Tap on LLDB startup command点击LLDB启动命令
  4. Tap +点按 +
  5. Add you command -http-proxy http://168.192.1.2:3300添加你的命令-http-proxy http://168.192.1.2:3300

在此处输入图片说明

that`s it.就是这样。

More cool stuff if you wanna use your PC IP, use this command:如果你想使用你的 PC IP 更酷的东西,使用这个命令:

  • -http-proxy "$(ipconfig getifaddr en0)":8888 on MacOS -http-proxy "$(ipconfig getifaddr en0)":8888在 MacOS 上
  • -http-proxy "$(hostname -i)":8888 on Linux -http-proxy "$(hostname -i)":8888在 Linux 上

I had no luck until I tried setting the environment variable http_proxy在我尝试设置环境变量http_proxy之前,我没有运气

http://developer.android.com/tools/help/emulator.html http://developer.android.com/tools/help/emulator.html

"If the -http-proxy command is not supplied, the emulator looks up the http_proxy environment variable and automatically uses any value matching the format described above." “如果未提供 -http-proxy 命令,模拟器将查找 http_proxy 环境变量并自动使用与上述格式匹配的任何值。”

For setting proxy server we need to set APNS setting.为了设置代理服务器,我们需要设置APNS设置。 To do this:去做这个:

  1. Go to Setting进入设置

  2. Go to wireless and networks转到无线和网络

  3. Go to mobile networks进入移动网络

  4. Go to access point names.转至接入点名称。 Use menu to add new apns使用菜单添加新的 apns

    Set Proxy = localhost设置代理 = 本地主机

    Set Port = port that you are using to make proxy server, in my case it is 8989设置端口 = 您用来制作代理服务器的端口,在我的例子中是 8989

    For setting Name and apn here is the link: 设置 Name 和 apn 这里是链接:

    According to your sim card you can see the table根据您的sim卡,您可以看到表格

Install Proxifier in your host computer.在您的主机上安装Proxifier Setup proxifier to use your proxy .设置 proxifier 以使用您的代理 You don't need to do anything else.你不需要做任何其他事情。 You will be fine.你会好起来的。 Proxifier traps the calls from the system (including the android emulator) and route it through the configured proxy. Proxifier 会捕获来自系统(包括 android 模拟器)的调用,并通过配置的代理进行路由。

Sometime even after setting all it may not work.有时即使在设置完所有内容后也可能无法正常工作。 I have tried all the methods like我已经尝试了所有的方法,比如

  1. Setting the proxy in Emulator APN在 Emulator APN 中设置代理
  2. Setting it thru eclipse preferences --> Android --> Launch通过 eclipse 首选项进行设置 --> Android --> 启动

Nothing worked.没有任何效果。 Then I did the following which worked instantly.然后我做了以下工作,立即生效。

Goto eclipse Run --> run configurations.转到 eclipse 运行 --> 运行配置。 Under Android Applications you can see you application.在 Android 应用程序下,您可以看到您的应用程序。 Now, on the right hand side click on the Target tab.现在,在右侧单击目标选项卡。 Under the 'Additional Emulator Command line options' add the following.在“附加模拟器命令行选项”下添加以下内容。

-dns-server < DNS servers from your local machine upto three > -http-proxy http://< your proxy >:< your proxy port > -dns-server <你本地机器上最多三个的DNS服务器> -http-proxy http://<你的代理>:<你的代理端口>

The catch here is that the DNS Server setting should be from your local system.这里的问题是 DNS 服务器设置应该来自您的本地系统。 Goto cmd prompt and run ipconfig to check your DNS servers.转到cmd提示符并运行ipconfig以检查您的 DNS 服务器。 Same with the proxy server and port.与代理服务器和端口相同。 Whatever works for your browser should be put in here.任何适用于您的浏览器的内容都应放在此处。

Easiest way is to delete default APN from emulator(in my case its T- mobile) and create new APN with your proxy settings.最简单的方法是从模拟器(在我的例子中是 T-mobile)中删除默认 APN,并使用您的代理设置创建新的 APN。

Note: i have tried all command line options and also tried setting the proxy for emulators default APN but nothing worked.注意:我尝试了所有命令行选项,还尝试为模拟器默认 APN 设置代理,但没有任何效果。

Depending on which environment you are using to run the emulator, check the logs to see how the emulator is started.根据您用于运行模拟器的环境,检查日志以了解模拟器是如何启动的。 Mine is started as:我的开始是:

C:\\Users\\johan\\AppData\\Local\\Android\\Sdk\\tools\\emulator.exe -netdelay none -netspeed full -avd Nexus_5X_API_23 C:\\Users\\johan\\AppData\\Local\\Android\\Sdk\\tools\\emulator.exe -netdelay none -n​​etspeed full -avd Nexus_5X_API_23

Then you add the -http-proxy option, in my case:然后你添加 -http-proxy 选项,在我的例子中:

C:\\Users\\johan\\AppData\\Local\\Android\\Sdk\\tools\\emulator.exe -netdelay none -netspeed full -avd Nexus_5X_API_23 -http-proxy 192.168.0.22:8888 C:\\Users\\johan\\AppData\\Local\\Android\\Sdk\\tools\\emulator.exe -netdelay none -n​​etspeed full -avd Nexus_5X_API_23 -http-proxy 192.168.0.22:8888

nothin of that worked i am using eclipse on windows 64-bit: do the folllowing steps... it worked for me: Window -> Preferences -> Android -> Launch -> Default Emulator Options -http-proxy="http://10.1.8.30:8080"我在 Windows 64 位上使用 eclipse 没有任何效果:执行以下步骤...它对我有用:Window -> Preferences -> Android -> Launch -> Default Emulator Options -http-proxy="http:/ /10.1.8.30:8080"

in your eclipse window在你的日食窗口

In case if you are under proxy environment and internet is not running in your emulator, then please don't change any setting in emulator.如果您在代理环境下并且模拟器中没有运行互联网,那么请不要更改模拟器中的任何设置。 Go to your eclipse project, right click , click on "Run as" then click on "Run Configuration".转到您的 eclipse 项目,右键单击 ,单击“运行方式”,然后单击“运行配置”。 In pop up window choose "Target" and scroll down a little, you will find "Additional Emulator Command Line Options" Enter your proxy setting here in "Additional Emulator Command Line Options" as i entered在弹出窗口中选择“目标”并向下滚动一点,您会发现“附加模拟器命令行选项”在我输入的“附加模拟器命令行选项”中输入您的代理设置

-http-proxy http://ee11s040:Om1l2ng3d4n2!08@hproxy.iitm.ac.in:3128 -http-proxy http://ee11s040:Om1l2ng3d4n2!08@hproxy.iitm.ac.in:3128

在此处输入图片说明

Then start a new Emulator.然后启动一个新的模拟器。

the best way to set corporate proxy with ntlm authentication is to use cntlm:使用 ntlm 身份验证设置公司代理的最佳方法是使用 cntlm:

http://cntlm.sourceforge.net/ http://cntlm.sourceforge.net/

Install and configure in C:\\Program Files\\Cntlm\\cntlm.ini By default cntlm, listens to 127.0.0.1:3128安装配置在C:\\Program Files\\Cntlm\\cntlm.ini 默认cntlm,监听127.0.0.1:3128

In android device set a new APN with proxy host 10.0.2.2 and port 3128 10.0.2.2 is a special alias to your host loopback interface (127.0.0.1 on your development machine)在 android 设备中设置一个新的 APN,代理主机为 10.0.2.2 和端口 3128 10.0.2.2 是主机环回接口的特殊别名(开发机器上的 127.0.0.1)

see also http://developer.android.com/tools/devices/emulator.html#emulatornetworking另见http://developer.android.com/tools/devices/emulator.html#emulatornetworking

Regards问候

Are you sure that your address is 168.192.1.2 and not 192.168.1.2?您确定您的地址是 168.192.1.2 而不是 192.168.1.2?

Notice the swapped first two numbers.注意交换的前两个数字。

在控制台中启动下一个命令:

emulator -avd emulator_name -http-proxy you_proxy_ip_address:8080

For some leanback (TV) emulators you can use cmd:对于某些leanback (TV) 模拟器,您可以使用 cmd:

adb shell settings put global http_proxy 10.0.2.2:8888

  • 8888 - is a port of proxy on a local machine (host), so on a local machine the http proxy will be 127.0.0.1:8888 8888 - 是本地机器(主机)上的代理端口,因此在本地机器上,http 代理将是 127.0.0.1:8888

To remove proxy (run sequentially in cmd line):要删除代理(在 cmd 行中按顺序运行):

adb shell settings delete global http_proxy

adb shell settings put global global_http_proxy_host ""

adb shell settings put global global_http_proxy_port ""

Having the AVD android emulator:拥有 AVD 安卓模拟器:

  1. Open the simulator ( "..\\android-sdk\\AVD Manager.exe")打开模拟器(“..\\android-sdk\\AVD Manager.exe”)
  2. Go to Tools转到工具
  3. Go to Options转到选项
  4. On Proxy settings:关于代理设置:

On the first field(HTTP Proxy Server) set only the IP address where is your proxy (XXX.XXX.XXX.XXX) on the second field set the port of your proxy (example: 8080)在第一个字段(HTTP 代理服务器)上只设置您的代理的 IP 地址(XXX.XXX.XXX.XXX)在第二个字段上设置您的代理的端口(例如:8080)

Then, click Close on the window and start the emulator然后,单击窗口上的关闭并启动模拟器

---- Added ... Then the alex steps works on my case: ---- 添加...然后亚历克斯步骤适用于我的情况:

Click on Menu
Click on Settings
Click on Wireless & Networks
Go to Mobile Networks
Go to Access Point Names
Here you will Telkila Internet (or other name), click on it.
In the Edit access point section, input the "proxy" and "port"

You can set the proxy in your app.您可以在您的应用程序中设置代理。 This can be done using Settings class.这可以使用 Settings 类来完成。 For example you can add following line to your "onCreate" method.例如,您可以在“onCreate”方法中添加以下行。

 Settings.System.putString(getContentResolver(), Settings.System.HTTP_PROXY, "myproxy:8080"); 

To change the proxy settings you have to have the android.permission.WRITE_SETTINGS permission in your AndroidManifest.xml file.要更改代理设置,您必须在 AndroidManifest.xml 文件中拥有 android.permission.WRITE_SETTINGS 权限。

For 2022 you can use adb command like below:对于 2022,您可以使用如下 adb 命令:

adb shell settings put global http_proxy "your_PC_IP:PORT_YOU_LISTEN"

and you can disable your proxy with command below:您可以使用以下命令禁用您的代理:

adb shell settings put global http_proxy :0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM