Android HttpURLConnection or httpclient always timeout, the server is OK, but the network is OK, the browser can surf the internet

Source: Internet
Author: User

============ Problem Description ============


HttpURLConnection conn = (httpurlconnection) u.openconnection ();
Conn.setdoinput (TRUE);
Conn.setdooutput (TRUE);
Conn.setconnecttimeout (10000);
Conn.setrequestmethod ("POST");
Conn.setrequestproperty ("Response-type", "JSON");//Add message headers

Conn.connect ();
DataOutputStream out = new DataOutputStream (Conn.getoutputstream ());
StringBuilder data = new StringBuilder ("");
for (int i=0;i<keys.length;i++) {
if (values[i]! = NULL &&! "". Equals (Values[i])) {
Tblog.info ("Key:" +keys[i]+ "values:" +values[i]);
Data.append (Keys[i]). Append ("="). Append (Urlencoder.encode (values[i), "UTF-8"). Append ("&");
}
}
if (Data.length () >0) {
data = Data.deletecharat (Data.length ()-1);
}
Tblog.info ("Data:" +data.tostring ());
Out.write (Data.tostring (). GetBytes ());
Out.flush ();
Out.close ();
if (Conn.getresponsecode () ==200) {
InputStream is = Conn.getinputstream ();
Return convertstreamtostring (IS);
}

============ Solution 1============


Well, is this a timeout? I'm not even supposed to.
You enter 180.186.31.33:8080 in the browser to try to open no

============ Solution 2============


/**
* Connection Timeout time
*/
Public final static int connect_timeout = 10000;
/**
* Read Timeout time
*/
Public final static int read_timeout = 30000;

Connmanagerparams.setmaxtotalconnections (Httpparams,
Max_total_connections);
Set the maximum wait time to get a connection
Httpconnectionparams.setconnectiontimeout (Httpparams,
Connect_timeout);
Httpconnectionparams.setsotimeout (Httpparams, read_timeout);

Set these three parameters to uppercase try

Android HttpURLConnection or httpclient always timeout, the server is OK, but the network is OK, the browser can surf the internet

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.