Android Tutorial: Create a Weather App with Yahoo Weather API – Part 1 of 3



UPDATE: Get the latest app code with weather forecast http://www.digitalphantom.net/PhantomWeather

Learn how to create an Android app using a third party API. In this tutorial we will be using Yahoo Weather API and will be using Object Orienter Programming Skills, dependecy injectiom, asynchronous task and others things.

In this first part we create the create the classes and interface that will be implemented in the later in the series.

Project Source:
https://github.com/DigitalPhantom/PhantomWeatherAndroid

Weather Icons by (VClouds):
http://vclouds.deviantart.com/art/VClouds-Weather-Icons-179152045

Yahoo Weather API:
https://developer.yahoo.com/weather/

For more tutorials:
http://digitalphantom.net/

source

#USA #News #USnews #worldnews #headlinenews #Breakingnews #weathernews #UnitedStates news #CanadianNews #weatherforecast top news #stories,Us news today #hourlyweather forecast

Lightning fast hosting for a shockingly low price! 50% off GoDaddy Hosting plans






37 Comments to Android Tutorial: Create a Weather App with Yahoo Weather API – Part 1 of 3

  1. Soni Akshay says:

    Hello sir, grt tutuorial.
    well i am having some minor problem right there, when i click on the location icon it keeps loading and doesn't show anything, quick reply will be appreciated…!!!!

  2. Aman Singh says:

    Hey, great tutorial! I just had one question about the updates wether app. As soon as I start my app, it asks me to either go geolocation or manual. I chose manual and press the back button but my app shuts down! Do you have fix for this? Thanks

  3. Getting an error ….plz help
    FATAL EXCEPTION: main
                                                                                   Process: com.example.saumrit1995.weather, PID: 2539
                                                                                   java.lang.NullPointerException: Attempt to invoke virtual method 'org.json.JSONObject org.json.JSONObject.optJSONObject(java.lang.String)' on a null object reference
                                                                                       at com.example.saumrit1995.weather.data.Chanel.popula(Chanel.java:23)
                                                                                       at com.example.saumrit1995.weather.service.Yahooweatherservice$1.onPostExecute(Yahooweatherservice.java:73)
                                                                                       at com.example.saumrit1995.weather.service.Yahooweatherservice$1.onPostExecute(Yahooweatherservice.java:34)

  4. Naomi Rison says:

    um I'm getting an error code:404 whenever I copy and paste hats in the image address in my tab

  5. Anil kumar says:

    Dragging and dropping widgets in xml is not a good practice , especially when you are making a video on youtube .LinearLayout would have been better option .

  6. Tom Hall says:

    Did you ever end up doing an upgraded app with forecasts?

  7. how do i implement code for 5 day forecast?

  8. Hey man i want to use recycler view in this tutorial. can u guide me?

  9. Devesh Beri says:

    There should have been an option to enter the location inside the app. :/

  10. Great Tutorial Thanks For That.

    I Have 1 Query….

    How Can I Get Weather At GPS Coordinates instead of City Name.

    Thank You

  11. Thanks Sir…… Good Tutorials and Explanation………….But i Got this Error i have checked the code many times and also matched with your code but i am getting this error……Kindly Help Me…… I'll be Thankful to you.

    07-03 01:28:26.129 7696-7696/com.logic2magic.weather E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.logic2magic.weather, PID: 7696
    android.content.res.Resources$NotFoundException: Resource ID #0x0
    at android.content.res.Resources.getValue(Resources.java:1351)
    at android.content.res.Resources.getDrawable(Resources.java:804)
    at android.content.res.Resources.getDrawable(Resources.java:771)
    at com.logic2magic.weather.MainActivity.serviceSuccess(MainActivity.java:48)
    at com.logic2magic.weather.services.YahooWeatherService$1.onPostExecute(YahooWeatherService.java:103)
    at com.logic2magic.weather.services.YahooWeatherService$1.onPostExecute(YahooWeatherService.java:43)
    at android.os.AsyncTask.finish(AsyncTask.java:651)
    at android.os.AsyncTask.-wrap1(AsyncTask.java)
    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

    The code of my YahooWeatherService.java File is Following:

    public class YahooWeatherService {

    private WeatherServiceCallback callback;
    private String location;
    private Exception error;

    public YahooWeatherService(WeatherServiceCallback callback)
    {
    this.callback = callback;
    }

    public String getLocation() {
    return location;
    }

    public void refreshWeather (String l)
    {

    this.location = l;

    new AsyncTask<String, Void, String>()
    {
    @Override
    protected String doInBackground(String… strings) {

    String YQL = String.format("select * from weather.forecast where woeid in (select woeid from geo.places(1) where text="%s")", strings[0]);
    String endpoint = String.format("https://query.yahooapis.com/v1/public/yql?q=%s&format=json", Uri.encode(YQL));

    try
    {
    URL url = new URL(endpoint);

    URLConnection connection = url.openConnection();

    InputStream inputStream = connection.getInputStream();

    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
    StringBuilder result = new StringBuilder();
    String line;
    while ((line = reader.readLine()) != null)
    {
    result.append(line);
    }

    return result.toString();
    }
    catch (Exception e)
    {
    error = e;
    }

    return null;
    }

    @Override
    protected void onPostExecute(String s) {

    if(s == null && error != null)
    {
    callback.serviceFailure(error);
    return;
    }

    try
    {
    JSONObject data = new JSONObject(s);

    JSONObject queryResults = data.optJSONObject("query");

    int count = queryResults.optInt("count");

    if(count == 0)
    {
    callback.serviceFailure(new LocationWeatherException("No weather information found for " + location));
    return;
    }

    Channel channel = new Channel();
    channel.populate(queryResults.optJSONObject("results").optJSONObject("channel"));

    callback.serviceSuccess(channel);
    }
    catch (Exception e)
    {
    callback.serviceFailure(e);
    }
    }
    }.execute(location);
    }

    public class LocationWeatherException extends Exception
    {
    public LocationWeatherException(String detailMessage)
    {
    super(detailMessage);
    }

    }

    }

  12. Loved the tutorial. Please do a full blown version of this app. Many thanks!

  13. Martyboy X5 says:

    hey, is there a way to show this via the XML feed instead of JSON?

  14. Usman Ali says:

    Your Videos are very informative thank you for these videos,i want to make some android app and submit it on app store kindly give your suggestion which app should i make that can be submitted to store easy.to make

  15. Hey, great tutorial but did you know how to get the conditions in german?

  16. John Evu says:

    I notice lots of people keep on speaking about Devomobiron Secrets (just search it on google). But I'm uncertain if it is good. Have you ever tried this popular make money from iphone & android apps step by step?

  17. Hi!This tutorial is amazing.However could you do a tutorial on a full blown weather app as you were saying? it will help alot also! thank you

  18. Anernermus says:

    You taught literally nothing about how to use API's… terrible tutorial. You simply demonstrated how to implement JUST the Yahoo Weather app.

  19. Hi Sir,
    I'm getting problem like unfortunately stopped working

    03-14 22:49:42.797 31481-31481/com.example.sikandaryadav.agrosist W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x416dfc08)
    03-14 22:49:42.797 31481-31481/com.example.sikandaryadav.agrosist E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.sikandaryadav.agrosist, PID: 31481
    java.lang.NullPointerException
    at com.example.sikandaryadav.agrosist.service.YahooWeatherService$1.onPostExecute(YahooWeatherService.java:81)
    at com.example.sikandaryadav.agrosist.service.YahooWeatherService$1.onPostExecute(YahooWeatherService.java:35)
    at android.os.AsyncTask.finish(AsyncTask.java:632)
    at android.os.AsyncTask.access$600(AsyncTask.java:177)
    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:146)
    at android.app.ActivityThread.main(ActivityThread.java:5511)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
    at dalvik.system.NativeStart.main(Native Method)

  20. is it REST architecture or SOAP?

  21. can any one tell how to add weather forcast………

  22. Cesar Garcia says:

    I NEED YOUR HELP, I WANT TO ADD THE FORECAST (TOMORROW & AFTER TOMORROW) I SEE THAT THE API HAVE THIS DATA, HOW CAN I DO IT?

  23. can i use your code in my Uni project and i manually set it some location if i dont do that it just stays in loading

  24. Thanks for your tutorial !
    But I have the following error in the Toast after loading : "/data/data/mypackageName/files/weather.data: open failed: ENOENT (No such file or directory) . How can I do to fix that ?

  25. Lucila M says:

    Hi. Excuse me, they know how to make my temperature display in ° C and not in F°?

  26. first of all, great tutorial!:D.
    Now i want to add the forecast daily. I need to change the String YQL and endpoint with something else or work just fine now with the present code?Thanks man!

  27. Nebs K says:

    Hi DF,
    Thanks very much for this tutorial. However, small question. How do I deal with following "condition" attribute – "Unknown":
    <yweather:condition xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="3200" date="Tue, 03 Nov 2015 9:59 pm CET" temp="41" text="Unknown"/>
    If I chose the location*** which is not having the "condition" attribute clearly defined the app is trying to read the condition and assign the icon but as it is "Unknown" the app is crashing. How to get around this issue?

    * https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D"Lausanne%2C%20CH")

    Many thanks again

  28. Mike Napoli says:

    Very good tutorial! This inspired me to make my own weather app. If you have time you should check it out and this tutorial helped a lot. It's called What To Wear Weather Free https://play.google.com/store/apps/details?id=senitexmobile.com.whattowearweather

  29. infantryman says:

    Hey, why in chrome yahoo api for you shows nicely? For me showing like a text in notepad not like programing code.

Leave a Reply

Your email address will not be published. Required fields are marked *