2011-12-22 66 views
0
public void consumeIt(){ 



    HttpConnection con = null; 
    InputStream is = null; 

    try { 
     String url = "http://where.yahooapis.com/geocode?q=1600+Pennsylvania+Avenue,+Washington,+DC"; 
     System.out.println("poo" + url); 
     con = (HttpConnection) Connector.open(url); 
     final int responseCode = con.getResponseCode(); 
     if (responseCode != HttpConnection.HTTP_OK) { 
      System.out.println("response code:" + responseCode); 
     } 
     System.out.println("Works here"); 
     is = con.openInputStream(); 
     byte[] responseData = new byte[10000]; 
     int length = 0; 
     StringBuffer rawResponse = new StringBuffer(); 
     while (-1 != (length = is.read(responseData))) { 
      rawResponse.append(new String(responseData, 0, length)); 
     } 
     final String result = rawResponse.toString(); 
     System.out.println("result:" + result); 


    } 
    catch (final Exception ex) { 
     System.out.println("error:" + ex.getMessage()); 
    } 
    finally { 
     try { 
      is.close(); 
      is = null; 
      con.close(); 
      con = null; 
     } 
     catch(Exception e){} 
    } 



    } 

我發現上面的代碼應該抓住api返回的xml數據,但我似乎無法得到它的工作。用「poo」打印的系統顯示出來,但不顯示「Works here」。我不知道什麼是錯的。我在9700模擬器中運行它。黑莓:消耗一個寧靜的網絡服務

爲什麼連接到Web服務如此複雜!

+0

請嘗試下面的代碼,讓我知道它工作與否 –

回答

0

請閱讀BB運輸和網絡。

對於快速修復使用這個網址:

「http://where.yahooapis.com/geocode?q=1600+Pennsylvania+Avenue,+Washington,+DC;deviceside=true」

而不是你的。

0

我已經回答此類錯誤的發生,因爲網絡的擴展

只是看到這個答案這並採取在你的代碼的變化絕對會工作

請查看以下鏈接,並採取方向

https://stackoverflow.com/a/8575601/914111

導入兩個類到您的項目後,只需要改變你的代碼如下方式

公共無效consumeIt(){

HttpConnection con = null; 
InputStream is = null; 

try { 
    String url = "http://where.yahooapis.com/geocode?q=1600+Pennsylvania+Avenue,+Washington,+DC"; 
    System.out.println("poo" + url); 
    HttpConnectionFactory factory = new HttpConnectionFactory(0); 
    con = factory.getHttpConnection(url); 
    final int responseCode = con.getResponseCode(); 
    if (responseCode != HttpConnection.HTTP_OK) { 
     System.out.println("response code:" + responseCode); 
    } 
    System.out.println("Works here"); 
    is = con.openInputStream(); 
    byte[] responseData = new byte[10000]; 
    int length = 0; 
    StringBuffer rawResponse = new StringBuffer(); 
    while (-1 != (length = is.read(responseData))) { 
     rawResponse.append(new String(responseData, 0, length)); 
    } 
    final String result = rawResponse.toString(); 
    System.out.println("result:" + result); 


} 
catch (final Exception ex) { 
    System.out.println("error:" + ex.getMessage()); 
} 
finally { 
    try { 
     is.close(); 
     is = null; 
     con.close(); 
     con = null; 
    } 
    catch(Exception e){} 
} 



} 

我得到輸出作爲我的控制檯上像

result:<?xml version="1.0" encoding="UTF-8"?> <ResultSet version="1.0"><Error>0</Error><ErrorMessage>No error</ErrorMessage><Locale>us_US</Locale><Quality>87</Quality><Found>1</Found><Result><quality>85</quality><latitude>38.898717</latitude><longitude>-77.035974</longitude><offsetlat>38.898590</offsetlat><offsetlon>-77.035971</offsetlon><radius>500</radius><name></name><line1>1600 Pennsylvania Ave NW</line1><line2>Washington, DC 20006</line2><line3></line3><line4>United States</line4><house>1600</house><street>Pennsylvania Ave NW</street><xstreet></xstreet><unittype></unittype><unit></unit><postal>20006</postal><neighborhood></neighborhood><city>Washington</city><county>District of Columbia</county><state>District of Columbia</state><country>United States</country><countrycode>US</countrycode><statecode>DC</statecode><countycode>DC</countycode><uzip>20006</uzip><hash>B42121631CCA2B89</hash><woeid>12765843</woeid><woetype>11</woetype></Result></ResultSet> <!-- gws14.maps.sg1.yahoo.com uncompressed/chunked Thu Dec 22 21:22:38 PST 2011 --> <!-- wws2.geotech.sg1.yahoo.com uncompressed/chunked Thu Dec 22 21:22:38 PST 2011 --> 
0

你可以試試這個。

public void consumeIt() 
{ 
    HttpConnection con = null; 
    InputStream is = null; 
    String desiredEncoding = "ISO-8859-1"; 
    StringBuffer returnStringBuffer = new StringBuffer(); 
    OutputStream os=null; 
    try 
     { 
      String url = "http://where.yahooapis.com/geocode?q=1600+Pennsylvania+Avenue,+Washington,+DC"; 
      System.out.println("poo" + url); 
      HttpConnectionFactory factory = new HttpConnectionFactory(0); 
      con = factory.getHttpConnection(url); 
      final int responseCode = con.getResponseCode(); 
      if (responseCode != HttpConnection.HTTP_OK) 
      { 
       System.out.println("response code:" + responseCode); 
      } 
      System.out.println("Works here"); 
      is = con.openInputStream(); 
      int ch; 
      String contenttype = httpConnection.getHeaderField("Content-Type"); 
      if (contenttype != null) 
      { 
      contenttype = contenttype.toUpperCase(); 
      if (contenttype.indexOf("UTF-8") != -1) 
      { 
        desiredEncoding = "UTF-8"; 
      } 
      } 
      InputStreamReader isr = new InputStreamReader(inputStream,desiredEncoding); 
      while ((ch = isr.read()) != -1) 
      { 
       returnStringBuffer.append((char) ch); 
      } 
     result=returnStringBuffer.toString(); 
     System.out.println("Result........"+result); 
     } 
     catch (final Exception ex) 
     { 
      System.out.println("error:" + ex.getMessage()); 
     } 
     finally 
     { 
     try 
     { 
       is.close(); 
       is = null; 
       con.close(); 
       con = null; 
     } 
     } 
}