套接字TimeOutException - 连接超时

时间:2014-05-31 07:13:34

标签: java tomcat google-cloud-messaging

我正在尝试连接Tomcat服务器(存储在Config.java中的IP地址-192.168.1.120(静态变量APP_SERVER_URL))但是它给了我套接字超时异常。

如何解决它。请帮助。

但是当我

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;
  }
}

0 个答案:

没有答案