Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7867751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:47:32+00:00 2026-06-03T00:47:32+00:00

I’ve been working on a simple weather app – and I’m using the Google

  • 0

I’ve been working on a simple weather app – and I’m using the Google Weather API. I’m trying to download the XML file and build the UI based on the info in it.

I try using AsyncTask to download the XML file and then parse it – but something’s not working! When I run the code I don’t have any error / the app starts but the info from the xml file is not shown!

Here’s the code:

    package com.android.weatherApp;



public class WeatherAppActivity extends Activity {

    static String url="http://www.google.com/ig/api?weather=";
    static final String KEY_FORCAST_INFO="forcast_information";
    static final String KEY_CITY="city";
    static final String KEY_CURRENT_CONDITIONS="current_conditions";
    static final String KEY_CONDITION="condition";
    static final String KEY_TEMP="temp_c";
    static final String KEY_ICON_SRC="icon";
    static final String KEY_FORCAST_COND="forecast_conditions";
    static final String KEY_DAY_WEEK="day_of_week";
    static final String KEY_LOW_TEMP="low";
    static final String KEY_HIGH_TEMP="high";
    Document finalDoc;
    XMLParser xmlParser;
    private TextView currentWeather;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        downloadXMLFile task=new downloadXMLFile();
        task.execute("New+York");

    }

    private class downloadXMLFile extends AsyncTask<String,Void,String>
    {
        @Override
        protected String doInBackground(String... params) {

            String xml=null;

            try{
                DefaultHttpClient httpClient=new DefaultHttpClient();
                HttpPost httpPost=new HttpPost(url+params[0]);
                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity=httpResponse.getEntity();
                xml=EntityUtils.toString(httpEntity);

            }
            catch(UnsupportedEncodingException e)
            {
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return xml;
        }

        @Override
        protected void onPostExecute(String xmlString)
        {
            Document doc = null;
            DocumentBuilderFactory dbf= DocumentBuilderFactory.newInstance();

            try {
                DocumentBuilder db = dbf.newDocumentBuilder();
                InputSource is=new InputSource();

                is.setCharacterStream(new StringReader(xmlString));
                doc=db.parse(is);

            } catch (ParserConfigurationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SAXException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            finalDoc=doc;

            NodeList nl=finalDoc.getElementsByTagName(KEY_CURRENT_CONDITIONS);
              NodeList nl2=finalDoc.getElementsByTagName(KEY_FORCAST_COND);
              currentWeather=(TextView)findViewById(R.id.currentState);
              TextView currentTemp = (TextView)findViewById(R.id.tempC);
              TextView currentLocation=(TextView)findViewById(R.id.currentLocation);
              currentLocation.setText("Iasi");

              for(int i=0;i<nl.getLength();i++)
              {
                Element e= (Element)nl.item(i);
                String cs=xmlParser.getValue(e, KEY_CONDITION).toUpperCase();
                currentWeather.setText(cs);

                String temperature=xmlParser.getValue(e, KEY_TEMP);
                currentTemp.setText(temperature);

                String logoImg=xmlParser.getValue(e, KEY_ICON_SRC).toLowerCase();

                if(logoImg.equals("/ig/images/weather/sunny.gif"))
                {
                    ImageView img=(ImageView)findViewById(R.id.wImg);
                    img.setImageResource(R.drawable.sun);
                }

              }

              for(int i=0;i<nl2.getLength();i++)
              {
                Element e=(Element)nl2.item(i);
                TextView day1=(TextView)findViewById(R.id.day1);
                day1.setText("TOMORW");
                TextView day2=(TextView)findViewById(R.id.day2);
                TextView day3=(TextView)findViewById(R.id.day3);
                ImageView img1=(ImageView)findViewById(R.id.day1Logo);
                ImageView img2=(ImageView)findViewById(R.id.day2Logo);
                ImageView img3=(ImageView)findViewById(R.id.day3Logo);
                TextView highTemp1=(TextView)findViewById(R.id.day1MaxTemp);
                TextView highTemp2=(TextView)findViewById(R.id.day2MaxTemp);
                TextView highTemp3=(TextView)findViewById(R.id.day3MaxTemp);

                String d1=xmlParser.getValue(e, KEY_DAY_WEEK).toUpperCase();
                String t=xmlParser.getValue(e, KEY_HIGH_TEMP); //get the max value of temperature - Fahrenheit
                int tempF=Integer.parseInt(t); // make the string a number
                float tC=(float) ((5./9)*(tempF-32)); // calculate the Celsius value of temperature
                tempF=(int)tC; // get only the Int value
                t=Integer.toString(tempF); // transform the temperature value back to string

                switch(i)
                {
                case 1:
                    highTemp1.setText(t);
                    break;
                case 2:
                    highTemp2.setText(t);
                    day2.setText(d1);
                    break;
                case 3:
                    highTemp3.setText(t);
                    day3.setText(d1);
                    break;
                }
              }
        }

    }

Please if you can tell me what am I doing wrong.
Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T00:47:34+00:00Added an answer on June 3, 2026 at 12:47 am

    First, your url variable contains “http://www.google.com/ig/api?weather=” not “http://www.google.com/ig/api?weather=New+York” so the xml content is empty.

    Try something like this in doInBackground method :

    String currentUrl = url; 
    if(params.length>0 && params[0]!=null)currentUrl+= params[0];
    

    I see that you instanciate your Views inside a loop. You must instanciate them inside your onCreate method or onStart method. Doing this, you will instanciate these variables after your activity is created and you will gain performance.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a simple page using Google Maps API 3. My first. One marker
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have just tried to save a simple *.rtf file with some websites and
In my XML file chapters tag has more chapter tag.i need to display chapters
I am trying to render a haml file in a javascript response like so:
I'm parsing an XML file, the creators of it stuck in a bunch social
I am using Paperclip to handle profile photo uploads in my app. They upload
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have a jquery bug and I've been looking for hours now, I can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.