android httpurlconnection或者httpclient总是超时,服务器正常,但网络正常,浏览器可以上网
====================问题描述====================
HttpURLConnection conn = (HttpURLConnection) u.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setConnectTimeout(10000);
conn.setRequestMethod("POST");
conn.setRequestProperty("Response-Type", "json");// 添加消息头
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);
}
====================解决方案1====================
额 这是超时? 应该是连不上吧
你在浏览器里 输入 180.186.31.33:8080试试能打开不
====================解决方案2====================
/**
* 连接超时时间
*/
public final static int CONNECT_TIMEOUT = 10000;
/**
* 读取超时时间
*/
public final static int READ_TIMEOUT = 30000;
ConnManagerParams.setMaxTotalConnections(httpParams,
MAX_TOTAL_CONNECTIONS);
// 设置获取连接的最大等待时间
HttpConnectionParams.setConnectionTimeout(httpParams,
CONNECT_TIMEOUT);
HttpConnectionParams.setSoTimeout(httpParams, READ_TIMEOUT);
把这三个参数设置大写试试
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。