-2

I have just started to develop android application yesterday and having a great fun with that technology.

I need to create an application in which user needs to enter the city name in edittext and then the current temperature of that city is shown in the textview.

I want to use the following URL for that where i can easily add the city name. Currently here i am passing sydney.

http://www.google.com/ig/api?weather=sydney

Please help me regarding this.

Altaaf
  • 527
  • 3
  • 11
Sagar Rawal
  • 1,442
  • 2
  • 12
  • 39

3 Answers3

1

I guess you want to know how to use the XML document you get by calling the url?!

    DefaultHttpClient httpclient = new DefaultHttpClient();

    HttpGet httpget = new HttpGet(http://www.google.com/ig/api?weather=sydney);
    httpget.setHeader("Content-Type", "application/xml");

    ResponseHandler responseHandler = new BasicResponseHandler();
    String responseBody = httpclient.execute(httpget, responseHandler);

That is basically what you need. The response body should contain the xml document. There you should be able to get your temperature.

You can improve that by adding Credentials and other parameters to the HttpClient constructor. See for example http://massapi.com/class/ba/BasicHttpParams.html

Moritz
  • 266
  • 1
  • 8
1

The response to this call returns a XML. You need to decide between DOM and SAX to parse the xml response. To start with google on android xml parsing. I found this one link, there are more. All the best.

http://android-pro.blogspot.in/2011/07/parsing-xml-wit-dom-parser.html

Siddharth
  • 9,349
  • 16
  • 86
  • 148
0

Using SAX Parser you can do the following.

/* Replace blanks with HTML-Equivalent. */ //url = new URL(queryString.replace(" ", "%20"));

                     /* Get a SAXParser from the SAXPArserFactory. */
                     SAXParserFactory spf = SAXParserFactory.newInstance();
                     SAXParser sp = spf.newSAXParser();

                     /* Get the XMLReader of the SAXParser we created. */
                     XMLReader xr = sp.getXMLReader();

                     /*
                      * Create a new ContentHandler and apply it to the
                      * XML-Reader
                      */
                     GoogleWeatherHandler gwh = new GoogleWeatherHandler();
                     xr.setContentHandler(gwh);

                     /* Use HTTPClient to deal with the URL */ 
                     HttpClient httpclient = new DefaultHttpClient(); 
                     HttpGet httpget = new HttpGet(queryString.replace(" ", "%20")); 
                     Log.d(DEBUG_TAG, "executing request " + httpget.getURI()); 
                     // create a response handler 
                     ResponseHandler<String> responseHandler = new BasicResponseHandler();
                     Log.i("Respond Handler","Step 1");
                     String responseBody = httpclient.execute(httpget, responseHandler); 
                      Log.d(DEBUG_TAG, "response from httpclient:\n "+responseBody); 

                     ByteArrayInputStream is = new ByteArrayInputStream(responseBody.getBytes()); 
                     xr.parse(new InputSource(is)); 
                     Log.d(DEBUG_TAG, "parse complete"); 
                     // parse complete
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Jigar Pandya
  • 6,004
  • 2
  • 27
  • 45