2013-02-09 45 views
0
HttpConnection c = null; 
    InputStream is = null; 
    StringBuffer sb = new StringBuffer(); 
    String request = serverUrl; // + "?loc=" + location.getLat() + "," + location.getLng() + "data=" + message.getString(); 
    try { 
     c = (HttpConnection) Connector.open("http://www.google.com"); 
     c.setRequestMethod(HttpConnection.GET); //default 
     is = c.openInputStream(); // transition to connected! 
     int ch = 0; 
     for (int ccnt = 0; ccnt < 150; ccnt++) { // get the title. 
      ch = is.read(); 
      if (ch == -1) { 
       break; 
      } 
      sb.append((char) ch); 
      return sb.toString(); 
     } 
    } catch (IOException x) { 
     x.printStackTrace(); 
    } finally { 
     try { 
      is.close(); 
      c.close(); 
     } catch (IOException x) { 
      x.printStackTrace(); 
     } 
    } 
    System.out.println(sb.toString()); 
    return null; 

- 這條線(HttpConnection)Connector.open(「http://www.google.com」);不響應在上面的代碼

(HttpConnection) Connector.open("http://www.google.com"); 

不響應。我能做些什麼來解決這個問題?

+0

應用程序是否正在掛起(等待)該線?它等待多久?它是否超時並拋出異常?你是在設備上還是在模擬器上測試這個?你確定設備/模擬器有良好的網絡連接嗎? – Nate 2013-02-10 04:32:58

回答

0


嘗試以下

HttpConnection c = null; 
    InputStream is = null; 
    StringBuffer sb = new StringBuffer(); 
    String request = serverUrl; // + "?loc=" + location.getLat() + "," + location.getLng() + "data=" + message.getString(); 
    try { 
     c = (HttpConnection) Connector.open("http://www.google.com"); 
     c.setRequestMethod(HttpConnection.GET); //default 
     int status_code=c.getResponseCode(); 
     if(status_code!=HttpConnection.HTTP_OK) 
     { 
      //Show the failure message to user 
      return; 
     } 

     //Remaining part is same as your code 
0

在大量的手機,以及一些模擬器,你只能開在另一個線程的連接。也就是說,如果你嘗試連接主要的therad = UI線程它不會工作,並且不會告訴你任何東西。嘗試簡單地將您的方法包裝在新線程中

相關問題