2011-12-22 5 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"が表示され、 "ここで動作"は表示されません。私は何が間違っているのか分かりません。私はそれを9700シミュレータで実行しています。Blackberry:安らかなWebサービスを利用しています

なぜWebサービスに接続するのはとても複雑です!

+0

次のコードを試してみてください –

答えて

0

BB輸送とネットワークについてお読みください。クイックフィックス用の

このURL:

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

あなたの代わりに。私はすでにエラーのこのタイプを答える

0

がちょうど絶対にそれは

は、次のリンクをチェックして、指示をしてください動作します。このこの答えを見て、あなたのコードの変更を取るため、ネットワークの拡張の発生

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

プロジェクトに2つのクラスをインポートした後、次のようにコードを変更してください。

あなたはこれを試すことができ

ます。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){} 
} 



} 

iは

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; 
     } 
     } 
} 
関連する問題