Test HTTP Network Connection exception resolution using Android Real Machine

Source: Internet
Author: User

Recently in the School Android network programming JSON parsing encountered a lot of problems, one of which is the use of the HTTP protocol to connect the network is always wrong, tinkering for nearly two days, finally a little understanding, is now recorded as follows.

First, the problem description

First I deployed a Web project on the local tomcat server to complete the operation of the client connection request through the servlet of the Web project. Everything else went well, the URL was entered correctly on the Computer browser, and the Java project created on Eclipse was normally accessed. But once placed in the Android project, there is an error, the exception message is thrown: Failed to connect to localhost/127.0.0.1 (port) after 90000ms:isconnected Failed:eco Nnrefused, due to lack of experience can not see the error. I'm stupid. Through a sentence of the log statement of the test, fortunately, by constantly touch the error to the "int code = Connection.getresponsecode ()," This sentence, each time the execution of the place will throw an exception. Searched for "Why does using the Getresponsecode () method throw an exception?" ", basically most of the answers point to the" Android network connection cannot be performed on the main thread, and requires a different threading "point of knowledge. After the multi-threading, the problem remains.

Ii. Solutions

After a constant groping attempt, just at last the test succeeded. The original problem is on my URL. When we configure the Tomcat server locally, there are three ways to connect to the server via a browser, and I'll give you three forms of URLs with my local server as an example:

1. http://127.0.0.1:8088/jsonproject/images/picture.png

2. http://localhost:8088/jsonproject/images/picture.png

3. http://192.168.0.100/jsonproject/images/picture.png (IP address slightly modified)

All of the above three URLs can be accessed via the browser to the Picture.png image when connected to the Internet. Careful observation is not difficult to find, the difference is that the red font part, the 1th and 2nd URLs are local access, that is, the first two access methods do not need to go through the network. The third URL requires networking for normal access, meaning that anyone can access the Picture.png resource through the network using the URL as long as your server is open. When you use a real-time test, your real machine is the equivalent of a separate device from your PC, so you can only use a third URL when you need to connect to the server. That is, you need to use your real IP address. (Real IP address viewing mode: In the command line window input: ipconfig can see your IPv4 address).

Third, other

Because of the narrow knowledge, the server is just contact, so there are many descriptions may not be too accurate or appropriate. I hope you find that this article has inappropriate places to be able to put forward, I will be educated, learn together.



-----May 3, 2015 01:15:22

-----written in a dorm


Test HTTP Network Connection exception resolution using Android Real Machine

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.