Java 套接字TimeOutException连接超时
我试图连接Tomcat服务器(IP地址-192.168.1.120,存储在Config.java(静态变量APP_Server_URL)中),但它给了我套接字超时异常 如何解决它。请帮助 但是当我Java 套接字TimeOutException连接超时,java,tomcat,google-cloud-messaging,Java,Tomcat,Google Cloud Messaging,我试图连接Tomcat服务器(IP地址-192.168.1.120,存储在Config.java(静态变量APP_Server_URL)中),但它给了我套接字超时异常 如何解决它。请帮助 但是当我 public class ShareExternalServer { public String shareRegIdWithAppServer(Map<String, String> paramsMap) { String result = ""; try {
public class ShareExternalServer {
public String shareRegIdWithAppServer(Map<String, String> paramsMap) {
String result = "";
try {
URL serverUrl = null;
try {
serverUrl = new URL(Config.APP_SERVER_URL);
} catch (MalformedURLException e) {
Log.e("AppUtil", "URL Connection Error: "
+ Config.APP_SERVER_URL, e);
result = "Invalid URL: " + Config.APP_SERVER_URL;
}
StringBuilder postBody = new StringBuilder();
Iterator<Entry<String, String>> iterator = paramsMap.entrySet()
.iterator();
while (iterator.hasNext()) {
Entry<String, String> param = iterator.next();
postBody.append(param.getKey()).append('=')
.append(param.getValue());
if (iterator.hasNext()) {
postBody.append('&');
}
}
String body = postBody.toString();
Log.d("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&",body);
byte[] bytes = body.getBytes();
HttpURLConnection httpCon = null;
try {
httpCon = (HttpURLConnection) serverUrl.openConnection();
httpCon.setDoOutput(true);
httpCon.setUseCaches(false);
httpCon.setFixedLengthStreamingMode(bytes.length);
httpCon.setRequestMethod("POST");
httpCon.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded;charset=UTF-8");
httpCon.setConnectTimeout(20000);
OutputStream out = httpCon.getOutputStream();
out.write(bytes);
out.close();
int status = httpCon.getResponseCode();
Log.d("$$$$$$$$$$$$$$$$$$",String.valueOf(status));
/* if (status == 200) {
result = "RegId shared with Application Server. RegId: "
+ regId;
} else {
result = "Post Failure." + " Status: " + status+regId;
}*/
} finally {
if (httpCon != null) {
((HttpURLConnection) httpCon).disconnect();
}
}
} catch (IOException e) {
result = "Post Failure. Error in sharing with App Server.";
Log.e("AppUtil", "Error in sharing with App Server: " + e);
}
return result;
}
}
公共类ShareExternalServer{
公共字符串shareRegIdWithAppServer(映射参数映射){
字符串结果=”;
试一试{
URL服务器URL=null;
试一试{
serverUrl=新URL(Config.APP\u SERVER\u URL);
}捕获(格式错误){
Log.e(“AppUtil”,“URL连接错误:”
+Config.APP_SERVER_URL,e);
结果=“无效URL:”+Config.APP\u服务器\u URL;
}
StringBuilder postBody=新建StringBuilder();
迭代器迭代器=paramsMap.entrySet()
.iterator();
while(iterator.hasNext()){
Entry param=iterator.next();
postBody.append(param.getKey()).append('='))
.append(param.getValue());
if(iterator.hasNext()){
postBody.append('&');
}
}
字符串体=postBody.toString();
Log.d(&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&,正文);
byte[]bytes=body.getBytes();
HttpURLConnection httpCon=null;
试一试{
httpCon=(HttpURLConnection)serverUrl.openConnection();
httpCon.setDoOutput(真);
httpCon.setUseCaches(假);
httpCon.setFixedLengthStreamingMode(字节.长度);
httpCon.setRequestMethod(“POST”);
httpCon.setRequestProperty(“内容类型”,
“application/x-www-form-urlencoded;charset=UTF-8”);
httpCon.setConnectTimeout(20000);
OutputStream out=httpCon.getOutputStream();
out.write(字节);
out.close();
int status=httpCon.getResponseCode();
Log.d(“$$$$$$”,String.valueOf(status));
/*如果(状态==200){
result=“与应用程序服务器共享的RegId。RegId:”
+雷吉德;
}否则{
结果=“失败后。”+”状态:“+Status+regId;
}*/
}最后{
如果(httpCon!=null){
((HttpURLConnection)httpCon).disconnect();
}
}
}捕获(IOE异常){
结果=“Post失败。与应用程序服务器共享时出错。”;
Log.e(“AppUtil”,“与应用服务器共享时出错:”+e);
}
返回结果;
}
}
您的服务器是否在ip地址mentioend上运行?端口是什么?它是否在http端口80/https端口443上运行。如果您的tomcat应用程序正在另一个端口上运行,您需要添加它。另一种选择是使用telnet port检查服务是否可访问有时会建立连接,但大多数情况下会提供套接字超时异常连接时间。使用ping检查是否有网络丢失。说试试ping-n50,检查是否有网络丢失。即有多少数据包成功。正如你所说,有时它会连接,这看起来是一个网络问题。