2012-04-11 13 views
0

Ich versuche, HTTP-Anfrage auf Blackberry-Plattform, Mein Problem ist: Wenn ich die Verbindung über WiFi machen funktioniert es gut, aber wenn ich das Mobilfunknetz verwenden funktioniert es nicht und die VerbindungDie http-Verbindung einfrieren bei der Verwendung von Mobile Netwotk

einfrieren Hier ist mein Code

public static String getHttpUTFResponse(String url) { 
    url = addConnSuffex(url); 
    HttpConnection connection = null; 
    byte responseData[] = null; 
    try { 
     connection = (HttpConnection) new ConnectionFactory() 
       .getConnection(url).getConnection(); 
     int len = (int) connection.getLength(); 
     System.out.println(len); 
     if (len != -1) { 
      responseData = new byte[len]; 
      DataInputStream dis; 
      dis = new DataInputStream(connection.openInputStream()); 
      dis.readFully(responseData); 
      dis.close(); 
     } 
    } catch (IOException e) { 
     System.out.println("Connection Error"); 
    } finally { 
     if (connection != null) { 
      try { 
       connection.close(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      connection = null; 
     } 

    } 
    if (responseData != null) { 
     try { 
      return new String(responseData, "UTF-8"); 
     } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
      return null; 
     } 
    } else { 
     return null; 
    } 
} 

public static String addConnSuffex(String url) { 
    ServiceBook sb = ServiceBook.getSB(); 
    ServiceRecord[] records = sb.findRecordsByCid("WPTCP"); 
    String uid = null; 
    if (records == null) { 
     return url; 
    } 
    for (int i = 0; i < records.length; i++) { 
     if (records[i].isValid() && !records[i].isDisabled()) { 
      if (records[i].getUid() != null 
        && records[i].getUid().length() != 0) { 
       if ((records[i].getUid().toLowerCase().indexOf("wifi") == -1) 
         && (records[i].getUid().toLowerCase() 
           .indexOf("mms") == -1)) { 
        uid = records[i].getUid(); 
        break; 
       } 
      } 
     } 
    } 
    if (DeviceInfo.isSimulator()) { 
     return url; 
    } 
    if (uid != null) { 
     // open a WAP 2 connection 
     url = url + ";deviceside=true;ConnectionUID=" + uid; 
     System.out.println("**************** on conn" + url); 
    } else { 

     url = url + ";deviceside=true;interface=wifi"; 
     // Consider another transport or alternative action. 
    } 
    System.out.println("**************** on conn" + url); 
    return url; 
} 

vielen Dank

Antwort

Verwandte Themen