Android Weather Forecast - Google Weather API - Description

Tutorials with advanced 'difficulty' and more Lines of Code.

Thanks for your advice

Postby rautakari » Wed Feb 03, 2010 1:11 pm

You had a great sample. I used this information to develop an implementation of my own. Could not done it without.
rautakari
Freshman
Freshman
 
Posts: 2
Joined: Wed Feb 03, 2010 1:05 pm

Top

Linki

Postby rautakari » Wed Feb 03, 2010 1:12 pm

And here is the link for the developed site: London Weather
rautakari
Freshman
Freshman
 
Posts: 2
Joined: Wed Feb 03, 2010 1:05 pm

getting the weather forecast by lat and lon

Postby aswani » Tue Mar 09, 2010 6:50 am

Hi all,


i want to get weather forecast by providing latitude and longitude .


Thanks in advance

Aswan
aswani
Junior Developer
Junior Developer
 
Posts: 19
Joined: Tue Feb 09, 2010 12:41 pm

Houston, we have a problem!

Postby robo » Fri Mar 12, 2010 10:05 am

Hi all,

Please help me with this. Have just run this app and have:

java.lang.NoSuchMethodException: org.anddev.android.weatherforecast.views.SingleWeatherInfoView.<init>(android.content.Context, android.util.AttributeSet)

I am new here, so help me please.
robo
Once Poster
Once Poster
 
Posts: 1
Joined: Fri Mar 12, 2010 10:01 am

Postby toobsco42 » Fri Apr 02, 2010 4:01 am

I am trying to make an http request within android but i am getting a Java.IO.FileNotFound exception. This would suggest the url path is incorrect however i tried it in a browser and it works.

Here is my code:
//urlString is the properly formatted string
URL url = new URL(urlString);

/* 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*/
Handler myHandler = new Handler();
xr.setContentHandler(myHandler);

/* Parse the xml-data from our URL. */


InputStream iStream = url.openStream();
InputSource is = new InputSource(iStream);
xr.parse(is);

The exception is thrown on url.openStream(); All other requests i make are working in android. This includes another request i make within the same activity work. I have already set security permissions in the androidmanifest.xml file. What seems to be the problem?
toobsco42
Freshman
Freshman
 
Posts: 4
Joined: Sun Feb 07, 2010 2:19 am

Postby dhay » Sun Apr 04, 2010 11:20 am

jeffy2010 wrote:Hi MrSnowflake.

I have resolved the issue and the app is running fine now.

The super of SingleWeatherInfoView which was extending the LinearLayout class had a parameter of "Map", which is not available in the LinearLayout.

Removed the Map, and its working fine.

SingleWeatherInfoView.java - line 41
public SingleWeatherInfoView(Context context, AttributeSet attrs,
Map inflateParams) {
super(context, attrs, inflateParams);

Thank you so much.



Hi,

First of wall hello, I am new in the forum. I have been reading it but still not registered.

I am trying to run this tutorial but it is not running. When I modify this:

SingleWeatherInfoView.java - line 41
public SingleWeatherInfoView(Context context, AttributeSet attrs,
Map inflateParams) {
super(context, attrs, inflateParams);

In the main.xml I got NullpointerException:null, My main.xml is like follows:


Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4.    android:orientation="vertical"
  5.    android:layout_width="fill_parent"
  6.    android:layout_height="fill_parent"
  7.    android:background="@drawable/weather_background"
  8.    >
  9.     <TableLayout
  10.         android:orientation="horizontal"
  11.         android:layout_width="fill_parent"
  12.         android:layout_height="wrap_content"
  13.         android:stretchColumns="0"
  14.         >
  15.         <TableRow
  16.                         android:layout_width="fill_parent"
  17.                         android:layout_height="wrap_content">
  18.                         <TextView  
  19.                         android:layout_width="wrap_content"
  20.                         android:layout_height="wrap_content"
  21.                         android:text="Usage like: 'City, Country'"
  22.                         android:gravity="left"
  23.                         />
  24.                 <CheckBox android:id="@+id/chk_usecelsius"  
  25.                         android:layout_width="wrap_content"
  26.                                 android:layout_height="wrap_content"
  27.                                 android:text="Use °C (not °F)"
  28.                                 android:gravity="right"
  29.                 />
  30.         </TableRow>
  31.     </TableLayout>
  32.     <TableLayout
  33.         android:orientation="horizontal"
  34.         android:layout_width="fill_parent"
  35.         android:layout_height="wrap_content"
  36.         android:stretchColumns="0"
  37.         >
  38.         <TableRow
  39.                         android:layout_width="fill_parent"
  40.                         android:layout_height="wrap_content">
  41.                          
  42.                         <EditText android:id="@+id/edit_input"
  43.                         android:layout_width="wrap_content"
  44.                         android:layout_height="wrap_content"
  45.                         android:text="Schriesheim, Germany"
  46.                         android:singleLine="true"
  47.                         />
  48.                 <Button android:id="@+id/cmd_submit"
  49.                         android:layout_width="wrap_content"
  50.                         android:layout_height="wrap_content"
  51.                         android:text="OK"
  52.                 />
  53.         </TableRow>
  54.     </TableLayout>
  55.         <agenda.test.SingleWeatherInfoView
  56.     android:id="@+id/weather_today"
  57.     android:orientation="horizontal"
  58.     android:layout_width="fill_parent"
  59.     android:layout_height="wrap_content"
  60.     android:gravity="center_horizontal"
  61.     android:paddingTop="15px"
  62.      />        
  63.  
  64.     <TableLayout
  65.         android:orientation="horizontal"
  66.         android:layout_width="fill_parent"
  67.         android:layout_height="wrap_content"
  68.         android:stretchColumns="0,1,2,3"
  69.         android:paddingTop="15px"
  70.         >
  71.                 <TableRow
  72.                                 android:layout_width="fill_parent"
  73.                                 android:layout_height="wrap_content">
  74.                         <agenda.test.SingleWeatherInfoView
  75.                                 android:id="@+id/weather_1"
  76.                                 android:layout_width="wrap_content"
  77.                                 android:layout_height="wrap_content"
  78.                                 android:gravity="center_horizontal"
  79.                                 android:orientation="vertical"
  80.                                 />
  81.                         <agenda.test.SingleWeatherInfoView
  82.                                 android:id="@+id/weather_2"
  83.                                 android:layout_width="wrap_content"
  84.                                 android:layout_height="wrap_content"
  85.                                 android:gravity="center_horizontal"
  86.                                 android:orientation="vertical"
  87.                                 />
  88.                         <agenda.test.SingleWeatherInfoView
  89.                                 android:id="@+id/weather_3"
  90.                                 android:layout_width="wrap_content"
  91.                                 android:layout_height="wrap_content"
  92.                                 android:gravity="center_horizontal"
  93.                                 android:orientation="vertical"
  94.                                 />
  95.                         <agenda.test.SingleWeatherInfoView
  96.                                 android:id="@+id/weather_4"
  97.                                 android:layout_width="wrap_content"
  98.                                 android:layout_height="wrap_content"
  99.                                 android:gravity="center_horizontal"
  100.                                 android:orientation="vertical"
  101.                                 />
  102.                         </TableRow>
  103.     </TableLayout>
  104. </LinearLayout>
  105.  
  106.  
Parsed in 0.011 seconds, using GeSHi 1.0.8.4


In my case I put the project in agenda.test that's why it is changed. But I tried the same as the original one and still didn't run. This is how SingleWeatherInfoView looks like(I tried the original code changing the method, deleting the MAP also, but didn't run either so I was trying this code that I found here, in this same post, but I don't know if it is correct or not:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package agenda.test;
  3.  
  4. import java.io.BufferedInputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.net.URL;
  8. import java.net.URLConnection;
  9. import java.util.Map;
  10. import android.content.Context;
  11. import android.graphics.Bitmap;
  12. import android.graphics.BitmapFactory;
  13. import android.graphics.Typeface;
  14. import android.util.AttributeSet;
  15. import android.widget.ImageView;
  16. import android.widget.LinearLayout;
  17. import android.widget.TextView;
  18.  
  19. /**
  20.  * The View capable of showing a WeatehrIcon + a Temperature-TextView.
  21.  */
  22. @SuppressWarnings("unused")
  23. public class SingleWeatherInfoView extends LinearLayout {
  24.  
  25.      // ===========================================================
  26.      // Fields
  27.      // ===========================================================
  28.  
  29.      private ImageView myWeatherImageView = null;
  30.      private TextView myTempTextView = null;
  31.      private TextView TempDay =null;
  32.  
  33.      // ===========================================================
  34.      // Constructors
  35.      // ===========================================================
  36.  
  37.      public SingleWeatherInfoView(Context context) {
  38.           super(context);
  39.      }
  40.  
  41.      public SingleWeatherInfoView(Context context, AttributeSet attrs) {
  42.           super(context, attrs);
  43.           /* Setup the ImageView that will show weather-icon. */
  44.           this.myWeatherImageView = new ImageView(context);
  45.           this.myWeatherImageView.setImageDrawable(getResources().getDrawable(R.drawable.dunno));
  46.  
  47.           /* Setup the textView that will show the temperature. */
  48.           this.myTempTextView = new TextView(context);
  49.           this.myTempTextView.setText("?");
  50.           this.myTempTextView.setTextSize(16);
  51.           this.myTempTextView.setTextColor(0xFF000000);
  52.           this.myTempTextView.setTypeface(Typeface.create("Tahoma", Typeface.BOLD));
  53.           /* Setup the textView that will show the day of forecast. */
  54.           this.TempDay = new TextView(context);
  55.           this.TempDay.setText("");
  56.           this.TempDay.setTextSize(16);
  57.           this.TempDay.setTextColor(0xFF000000);
  58.           this.TempDay.setTypeface(Typeface
  59.                     .create("Tahoma", Typeface.BOLD));
  60.           /* Add child views to this object. */
  61.           this.addView(this.myWeatherImageView, new LinearLayout.LayoutParams(
  62.                     LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));                                                            
  63.           this.addView(this.myTempTextView, new LinearLayout.LayoutParams(
  64.                     LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
  65.           this.addView(this.TempDay, new LinearLayout.LayoutParams(
  66.                           LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
  67.      }
  68.  
  69.      // ===========================================================
  70.      // Getter & Setter
  71.      // ===========================================================
  72.  
  73.      public void reset() {
  74.           this.myWeatherImageView.setImageDrawable(getResources().getDrawable(R.drawable.dunno));
  75.           this.myTempTextView.setText("?");
  76.           this.TempDay.setText("");
  77.      }
  78.  
  79.      /** Sets the Child-ImageView of this to the URL passed. */
  80.      public void setRemoteImage(URL aURL) {
  81.           try {
  82.                URLConnection conn = aURL.openConnection();
  83.                conn.connect();
  84.                InputStream is = conn.getInputStream();
  85.                BufferedInputStream bis = new BufferedInputStream(is);
  86.                Bitmap bm = BitmapFactory.decodeStream(bis);
  87.                bis.close();
  88.                is.close();
  89.                this.myWeatherImageView.setImageBitmap(bm);
  90.           } catch (IOException e) {
  91.                /* Reset to 'Dunno' on any error. */
  92.                this.myWeatherImageView.setImageDrawable(getResources()
  93.                          .getDrawable(R.drawable.dunno));
  94.           }
  95.      }
  96.  
  97.      public void setTempCelcius(int aTemp) {
  98.           this.myTempTextView.setText("" + aTemp + " °C");
  99.      }
  100.  
  101.      public void setTempFahrenheit(int aTemp) {
  102.           this.myTempTextView.setText("" + aTemp + " °F");
  103.      }
  104.  
  105.      public void setTempFahrenheitMinMax(int aMinTemp, int aMaxTemp) {
  106.           this.myTempTextView.setText("" + aMinTemp + "/" + aMaxTemp + " °F");
  107.      }
  108.  
  109.      public void setTempCelciusMinMax(int aMinTemp, int aMaxTemp) {
  110.           this.myTempTextView.setText("" + aMinTemp + "/" + aMaxTemp + " °C");
  111.      }
  112.  
  113.      public void setTempString(String aTempString) {
  114.           this.myTempTextView.setText(aTempString);
  115.      }
  116.      
  117.      public void setDayofWeek(String dayofWeek) {
  118.                 this.TempDay.setText("" +dayofWeek);
  119.         }
  120.    
  121. }
  122.  
Parsed in 0.050 seconds, using GeSHi 1.0.8.4


I was trying to modify by myself the code, but I don't really know how to put the SingleWeatherInfoView in the main, it never finds the class.


Here I upload all the project (I commented the R.id.weather_1 and so on in the Weatherforecast.java to be able to compile, because it did not find them).

Thanks in advance,

dhay
Attachments
Copy of testAgenda.rar
Scanned with Avira Antivir Personal.
(195.31 KiB) Downloaded 339 times
dhay
Once Poster
Once Poster
 
Posts: 1
Joined: Sun Apr 04, 2010 11:06 am

Top

NULL Pointer Exception

Postby Dev » Thu Apr 15, 2010 4:53 am

I am getting NULL Pointer Exception while calling openStream APIs for google weather. I did give INTERNET permission in manifest file but still getting NULL pointer exception. Please can you help me.
Dev
Freshman
Freshman
 
Posts: 6
Joined: Fri Mar 26, 2010 11:05 am

Re: Android Weather Forecast - Google Weather API - Descript

Postby vivek_goel » Thu Jul 29, 2010 2:54 pm

Thanks alottttttttttttttttt Shalini...
u r owsome.
u made my day.
now its working fine.
vivek_goel
Freshman
Freshman
 
Posts: 3
Joined: Wed Jul 14, 2010 10:52 am

Re: Android Weather Forecast - Google Weather API - Descript

Postby edreamz » Wed Sep 22, 2010 8:59 pm

I am getting the following exception, not sure how to debug this . Any help is appreciated. :)

ERROR/WeatherForcaster(672): org.apache.harmony.xml.ExpatParser$ParseException: At line 1, column 190: not well-formed (invalid token)
User avatar
edreamz
Junior Developer
Junior Developer
 
Posts: 18
Joined: Fri Aug 06, 2010 2:29 am
Location: U.S

Re: getting the weather forecast by lat and lon

Postby edreamz » Thu Sep 23, 2010 7:22 pm

aswani wrote:Hi all,


i want to get weather forecast by providing latitude and longitude .


Thanks in advance

Aswan


Not sure if the Google Weather APIs are available, but you can make use of the Weather Bug APIs at: http://weather.weatherbug.com/corporate/products/API/help.aspx#wx

\m/
Peace
User avatar
edreamz
Junior Developer
Junior Developer
 
Posts: 18
Joined: Fri Aug 06, 2010 2:29 am
Location: U.S

Re: Android Weather Forecast - Google Weather API - Descript

Postby edreamz » Thu Sep 23, 2010 7:23 pm

edreamz wrote:I am getting the following exception, not sure how to debug this . Any help is appreciated. :)

ERROR/WeatherForcaster(672): org.apache.harmony.xml.ExpatParser$ParseException: At line 1, column 190: not well-formed (invalid token)


Worked fine, there seemed to be something wwrong with the input text.


\m/
Peace.
User avatar
edreamz
Junior Developer
Junior Developer
 
Posts: 18
Joined: Fri Aug 06, 2010 2:29 am
Location: U.S

Re: Android Weather Forecast - Google Weather API - Descript

Postby Dliswa » Fri Oct 15, 2010 7:30 pm

Hi plusminus !
Dear i m quite new to android .When i press the menu key of the emulator the menu does not appear.
can You fix it.
Plllzzzzzzzz

thanks in advance.


package com.android.example;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;

public class MenuExample extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

}

public boolean onCreatOptionMenu(Menu menu){
super.onCreateOptionsMenu(menu);
menu.add(0, 0, 0, "Hello");
menu.add(0, 1, 0, "World");
return true;
}
}
Dliswa
Developer
Developer
 
Posts: 30
Joined: Fri Oct 15, 2010 5:56 am

Re: Android Weather Forecast - Google Weather API - Descript

Postby edreamz » Fri Oct 15, 2010 10:49 pm

The function name is onCreateOptionsMenu and not onCreatOptionMenu as seen in your code.



Dliswa wrote:Hi plusminus !
Dear i m quite new to android .When i press the menu key of the emulator the menu does not appear.
can You fix it.
Plllzzzzzzzz

thanks in advance.


package com.android.example;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;

public class MenuExample extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

}

public boolean onCreatOptionMenu(Menu menu){
super.onCreateOptionsMenu(menu);
menu.add(0, 0, 0, "Hello");
menu.add(0, 1, 0, "World");
return true;
}
}
User avatar
edreamz
Junior Developer
Junior Developer
 
Posts: 18
Joined: Fri Aug 06, 2010 2:29 am
Location: U.S

Re: Android Weather Forecast - Google Weather API - Descript

Postby aniket.kulkarni » Wed Dec 08, 2010 7:16 am

hello plus minus,
This works absolutely fine on 1.5 and 2.2 sdk..but i am unable to run it on sdk 2.1update.....xr.parse(new InputSource(url.openStream())) gives wheather query error!!!

Kindly help if anybody knows this issue plz!!!
aniket.kulkarni
Freshman
Freshman
 
Posts: 9
Joined: Fri Nov 12, 2010 6:54 am

Re: Android Weather Forecast - Google Weather API - Descript

Postby reza » Tue Jan 25, 2011 8:28 pm

Hi.
I have been trying to use your code.
What I have been trying is just to get the temperature of a city. and print it in a textview. But it shows me error.
The error I can see in my emulator is : Error:www.google.com
Can you please help me to show where I am doing wrong??
public class WeatherForcast extends Activity {
// private static final String DEBUG_TAG = null;
private final String DEBUG_TAG = "WeatherForcaster";
/** Called when the activity is first created. */
String cityParam="Copenhagen";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

TextView tv=new TextView(this);
try{
URL url;
String queryString="http://www.google.com/ig/api?weather="+cityParam;

/* 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
*/
WeatherHandler myHandler=new WeatherHandler();
xr.setContentHandler(myHandler);

/* Parse the xml-data our URL-call returned. */
xr.parse(new InputSource(url.openStream()));

/* Our Handler now provides the parsed weather-data to us. */
WeatherSet myWeatherSet=myHandler.getWeatherSet();

/* Update the SingleWeatherInfoView with the parsed data. */
WeatherCurrentCondition wCurrent=new WeatherCurrentCondition();

Integer temp=wCurrent.getTempCelcius();

tv.setText(temp);
}catch(Exception ex){
tv.setText("Error: " + ex.getMessage());
Log.e(DEBUG_TAG, "WeatherQueryError", ex);
}
this.setContentView(tv);

}
}
reza
Freshman
Freshman
 
Posts: 9
Joined: Sat Jan 22, 2011 12:29 am

Top
PreviousNext

Return to Advanced Tutorials

Who is online

Users browsing this forum: No registered users and 3 guests