Monitor the HTTP of the android Simulator

Source: Internet
Author: User
Tags fiddler2

Problem scenario: When debugging an application, you need to monitor the HTTP Communication between the application and the server.

Solution: the official fiddler2.android documentation mentioned that it is good to use tcpdump to obtain communication packets or real-time monitoring. fiddler2 is the best HTTP monitoring tool on windows. It has friendly interfaces and functions. fiddler2 is easy to use. After running, a proxy (127.0.0.1: 8888 by default) is created on the local machine. Other applications that access HTTPProgram, Set the proxy to the same address and port.

Implementation process:

According to the instructions in the document, when starting the simulator, use the-http-proxy parameter and set 127.0.0.1: 8888 as the online proxy of the simulator. The result fails. fiddler2 does not catch the communication response.

Solution:

Read the simulator section in the document and see the network address space section.

The simulator runs behind the virtual router/Firewall Service. This router/Firewall Service is isolated from the network interface and settings of the development machine and is also isolated from the Internet. the simulated device cannot access the development machine or other simulators on the network. what it sees is that it connects to a vro/firewall over Ethernet.

This is clear, the simulator does not know what 127.0.0.1 is, nor can it be accessed. the proxy set on 127.0.0.1 naturally cannot catch any communication response. the document goes on to write that the virtual Firewall/router management ranges from 10.0.2.2 to 10.0.2.24, the simulator IP address is 10.0.2.15, and the development machine address seen by the simulator is 10.0.2.2. that is to say, from the simulator's standpoint, fiddler2 runs on 10.0.2.2.

Therefore, the emulator command is used to add the AVD-http-proxy 10.0.2.2: 8888 parameter to start the simulator. Run browser on the simulator and access any web address. You can see that fiddler2 has caught HTTP Communication:

JavaCode:

    1. Private httpurlconnection createconnection (URL ){
    2. Httpurlconnection conn = NULL;
    3. Try {
    4. Conn = (httpurlconnection) URL. openconnection ();
    5. } Catch (ioexception e ){
    6. E. printstacktrace ();
    7. }
    8. Return conn;
    9. }

Copy code

This time, fiddler2 failed to catch communications. However, the application successfully accesses the webpage. It is not enough to add the-http-proxy parameter when starting the simulator. When the application creates a connection, you must specify the HTTP Proxy:

Java code:

    1. Private httpurlconnection createconnectionwithproxy (URL, string proxyaddress, int proxyport ){
    2. Httpurlconnection conn = NULL;
    3. Proxy proxy = new proxy (proxy. type. HTTP, new inetsocketaddress (proxyaddress, proxyport ));
    4. Try {
    5. Conn = (httpurlconnection) URL. openconnection (proxy );
    6. } Catch (ioexception e ){
    7. E. printstacktrace ();
    8. }
    9. Return conn;
    10. }

Copy code

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.