黑莓:消耗一个宁静的网络服务

时间:2011-12-22 23:12:14

标签: blackberry

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服务这么复杂!

3 个答案:

答案 0 :(得分:0)

请阅读有关BB传输和网络的信息。

要快速修复,请使用此网址:

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

而不是你的。

答案 1 :(得分:0)

由于网络扩展

,我已经回答了此类错误

只需看到这个答案,并在代码中进行更改绝对可行

请检查以下链接并采取指示

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

将两个类导入项目后,只需按照以下方式更改代码

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);
   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 -->

答案 2 :(得分: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; 
         }
       }
 }