2012-06-07 78 views
1

我在某些客戶的手機上遇到了一個奇怪的問題。看起來,在離開服務區幾個小時之後,我的Android應用程序將失去訪問網絡的能力。其他應用程序(如Web瀏覽器或電子郵件)將能夠訪問Web,但不能訪問我的應用程序。Android應用無法通過網絡進行通信

唯一可以想象的解釋是,當沒有數據服務時,它以某種方式泄漏套接字。

這裏是我的代碼:

String sendWebPOST(String url, String pPostData) throws IOException { 
    AndroidHttpClient c = null; 
    InputStream is = null; 
    OutputStream os = null; 
    int rc; 
    String strResponse = null; 

    try {    
     c = AndroidHttpClient.newInstance("Mobile"); 

     // Set the request method and headers 
     HttpPost request = new HttpPost(url); 
     request.addHeader("Content-Type", "application/x-www-form-urlencoded"); 

     request.setEntity(new StringEntity(pPostData)); 

     try { 
      HttpResponse response = c.execute(request); 

      // Getting the response code will open the connection, 
      // send the request, and read the HTTP response headers. 
      // The headers are stored until requested. 
      rc = response.getStatusLine().getStatusCode(); 
      if (rc != HttpStatus.SC_OK) { 
       throw new IOException("HTTP response code: " + rc); 
      } 

      try { 
       is = response.getEntity().getContent(); 

       int ch; 
       StringBuffer sb = new StringBuffer(); 
       while ((ch = is.read()) != -1) { 
        sb.append((char) ch); 
       } 
       //if(sb.length() > 0) 
       strResponse = sb.toString(); 
      } 
      finally { 
       if (is != null) { 
        is.close(); 
       } 
      } 
     } 
     finally { 
      if (os != null) { 
       os.close(); 
      } 
     } 
    } 
    catch (ClassCastException e) { 
     throw new IllegalArgumentException("Not an HTTP URL"); 
    } 
    finally { 
     if (c != null) { 
      c.close(); 
     } 
    } 

    return strResponse; 
} 

這個函數被調用大約每隔十分鐘更新發送到遠程服務器。當應用程序進入這種怪異狀態時,用戶仍然能夠打開活動,與菜單進行交互等,以便應用程序仍在運行。但是,它無法通過網絡發送任何內容。

任何想法可能會發生什麼?

有問題的手機是在T-Mobile網絡上運行Android 2.3的myTouch 4G和三星Galaxy II。

謝謝。

回答

0

出於某種原因,使用HttpURLConnection而不是AndroidHttpClient炒鍋更好。一旦我升級了我的代碼,我就不會再報告任何問題了。

HttpURLConnection conn = null; 
    InputStream is = null; 
    OutputStream os = null; 
    String strResponse = null; 

    try { 
     conn = (HttpURLConnection)(new URL(url)).openConnection(); 

     conn.setRequestMethod("POST"); 
     conn.setRequestProperty("Connection", "close"); 
     conn.setRequestProperty("content-type", "application/x-www-form-urlencoded"); 
     conn.setConnectTimeout(30000); 
     conn.setDoInput(true); 
     conn.setDoOutput(true); 
     conn.setChunkedStreamingMode(0); 
     conn.connect(); 

     os = new BufferedOutputStream(conn.getOutputStream()); 

     os.write(pPostData.getBytes()); 
     os.flush(); 

     // Getting the response code will open the connection, 
     // send the request, and read the HTTP response headers. 
     // The headers are stored until requested. 
     int rc = conn.getResponseCode(); 
     if (rc != HttpURLConnection.HTTP_OK) { 
      throw new IOException("HTTP response code: " + rc); 
     } 

     is = new BufferedInputStream(conn.getInputStream()); 

     int ch; 
     StringBuffer sb = new StringBuffer(); 
     while ((ch = is.read()) != -1) { 
      sb.append((char) ch); 
     } 
     //if(sb.length() > 0) 
     strResponse = sb.toString(); 
    } 
    finally { 

     if (is != null) { 
      try { 
       is.close(); 
      } 
      catch (Exception ex) { 
       System.out.println("is.close ex " + ex); 
      } 
     } 
     if (os != null) { 
      try { 
       os.close(); 
      } 
      catch (Exception ex) { 
       System.out.println("os.close ex " + ex); 
      } 
     } 

     if (conn != null) { 
      try { 
       conn.disconnect(); 
      } 
      catch (Exception ex) { 
       System.out.println("disconnect ex " + ex); 
      } 
     } 
    } 
相關問題