2011-12-22 44 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; 
     } 
     } 
}