2011-04-04 93 views
0

以下代码来自Facebook Android SDK,用于与Facebook的所有交互。我正在使用它来搜索和/或张贴到持有唤醒锁的服务的Facebook。因此,如果手机以某种方式失去了互联网连接,我希望服务放弃并终止以避免浪费电池。如何停止HTTP连接,假设丢失连接?

我不明白的是如何打断这样的事情,因为这个问题可能发生在这个方法的任何一点。我也不知道HTTPUrlConnection中是否有连接超时的内置机制。

public static String openUrl(String url, String method, Bundle params) 
     throws MalformedURLException, IOException { 
    // random string as boundary for multi-part http post 
    String strBoundary = "3i2ndDfv2rTHiSisAbouNdArYfORhtTPEefj3q2f"; 
    String endLine = "\r\n"; 

    OutputStream os; 

    if (method.equals("GET")) { 
     url = url + "?" + encodeUrl(params); 
    } 
    Log.d("Facebook-Util", method + " URL: " + url); 
    HttpURLConnection conn = (HttpURLConnection) new URL(url) 
      .openConnection(); 
    conn.setRequestProperty("User-Agent", System.getProperties() 
      .getProperty("http.agent") + " FacebookAndroidSDK"); 
    if (!method.equals("GET")) { 
     Bundle dataparams = new Bundle(); 
     for (String key : params.keySet()) { 
      if (params.getByteArray(key) != null) { 
       dataparams.putByteArray(key, params.getByteArray(key)); 
      } 
     } 

     // use method override 
     if (!params.containsKey("method")) { 
      params.putString("method", method); 
     } 

     if (params.containsKey("access_token")) { 
      String decoded_token = URLDecoder.decode(params 
        .getString("access_token")); 
      params.putString("access_token", decoded_token); 
     } 

     conn.setRequestMethod("POST"); 
     conn.setRequestProperty("Content-Type", 
       "multipart/form-data;boundary=" + strBoundary); 
     conn.setDoOutput(true); 
     conn.setDoInput(true); 
     conn.setRequestProperty("Connection", "Keep-Alive"); 
     conn.connect(); 
     os = new BufferedOutputStream(conn.getOutputStream()); 

     os.write(("--" + strBoundary + endLine).getBytes()); 
     os.write((encodePostBody(params, strBoundary)).getBytes()); 
     os.write((endLine + "--" + strBoundary + endLine).getBytes()); 

     if (!dataparams.isEmpty()) { 

      for (String key : dataparams.keySet()) { 
       os.write(("Content-Disposition: form-data; filename=\"" 
         + key + "\"" + endLine).getBytes()); 
       os.write(("Content-Type: content/unknown" + endLine + endLine) 
         .getBytes()); 
       os.write(dataparams.getByteArray(key)); 
       os.write((endLine + "--" + strBoundary + endLine) 
         .getBytes()); 

      } 
     } 
     os.flush(); 
    } 

    String response = ""; 
    try { 
     response = read(conn.getInputStream()); 
    } catch (FileNotFoundException e) { 
     // Error Stream contains JSON that we can parse to a FB error 
     response = read(conn.getErrorStream()); 
    } 
    return response; 
} 

回答

1

文件说:

httpConn.setConnectTimeout(HTTP_CONNECT_TIMEOUT); 
httpConn.setReadTimeout(HTTP_READ_TIMEOUT); 

看到http://developer.android.com/reference/java/net/URLConnection.html#setConnectTimeout(int

+0

感谢。遇到这个问题。我在conn.connect()之前立即放置了上面的行,并将超时设置为1来测试它,但我仍然没有收到SocketTimeoutException。哪些方法实际上会抛出异常? connect()和getInputStream()的文档没有提及它。 – Tenfour04 2011-04-05 02:18:49

+0

在setConnectTimeout()下的Java 1.5文档中发现了这个问题:“此方法的某些非标准暗示可能会忽略指定的超时。要查看连接超时设置,请调用getConnectTimeout()。”当我在设置超时后以调试模式暂停时,连接实例中超时的成员变量为零,但调用getConnectTimeout()可返回我的值。不明白这里发生了什么。 – Tenfour04 2011-04-05 02:27:08

+0

尝试了好几次之后,我有一个实例获得了超时异常。当然,它不是连接和阅读2毫秒!! ??当我关掉wifi并依靠3G时,它每次都会发生。从未意识到连接可能会很快发生。 – Tenfour04 2011-04-05 02:35:41