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){} 
    } 



    }

I found the code above that is supposed to grab the xml data returned by the api, however I can't seem to get it working. The system print out with "poo" shows up, but not the "Works here." I have no idea what's wrong. I'm running it in a 9700 simulator.

Why is it so complicated to connect to a web service!

Adam
  • 8,849
  • 16
  • 67
  • 131

3 Answers3

0

Please read about BB transports and networking.

For quick fix use this url:

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

instead of yours.

Eugen Martynov
  • 19,888
  • 10
  • 61
  • 114
0

I already answer this type of errors occurred because of Network extensions

just see this answer this and take changes in your code absolutely it will work

please check the following link and take directions

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

after importing two classes into your project just change your code as following way

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 got output as on my console like

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 -->
Community
  • 1
  • 1
Govindarao Kondala
  • 2,862
  • 17
  • 27
0

You can try this.

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; 
         }
       }
 } 
Deepika
  • 566
  • 2
  • 10