如果天气会下雨,请通知我

时间:2013-08-01 05:23:03

标签: android

大家好我想问一下,我怎么能通知我的手机 如果你发送当前天气的请求会下雨 请求应该在后台运行,如果下雨,它会通知。

任何人都可以为我提供一些提示或任何教程 能够通过服务发送请求并通知我 若下雨请分享一些想法

因为我真的需要它,谢谢你。

我已经完成了请求当前位置的课程,我使用openweathermap API来获取当前天气。

package com.example.autoapp;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject;

public class WeatherJSONParser {

    public WeatherJSONParser()

    {

    }

    public JSONObject getWeatherFromUrl(String url)
    {
        String holder= null;
        JSONObject jobj=null;
        try
        {
            HttpClient client= new DefaultHttpClient();
            HttpGet get= new HttpGet(url);
            HttpResponse response= client.execute(get);
            StatusLine sLine= response.getStatusLine();
            int status= sLine.getStatusCode();


            if(status==200)

            {
                HttpEntity content= response.getEntity();
                InputStream iStream= content.getContent();
                StringBuilder cBuilder= new StringBuilder();
                BufferedReader bReader= new BufferedReader(new InputStreamReader(iStream));
                String cLine=null;
                while((cLine=bReader.readLine())!= null)
                {
                    cBuilder.append(cLine);
                }
                iStream.close();
                holder= cBuilder.toString();
                jobj= new JSONObject(holder);


                return jobj;
            }

        }

        catch(Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }


}

1 个答案:

答案 0 :(得分:1)

    private final String URL = "http://api.worldweatheronline.com/free/v1/weather.ashx?key=***your api key*****&q=00.00,00.00&cc=no&date=2010-04-23&format=xml";
        private static final String KEY_SONG = "weather"; // parent node

        // public variable
        public static final String KEY_TEMPERATURE_MAXIMUM = "tempMaxC";
        public static final String KEY_TEMPERATURE_MINIMUM = "tempMinC";
        public static final String KEY_WEATHER_DESCRIPTION = "weatherDesc";
        public static final String KEY_PRECIPITATION = "precipMM";
        public static final String KEY_THUMB_URL = "weatherIconUrl";
    private  ArrayList<HashMap<String, String>> aList =null;

@Override
    protected void onCreate(Bundle savedInstanceState) {
new LongOperation().execute("");
}


 private class LongOperation extends AsyncTask<String, Void, String> {

         @Override
         protected String doInBackground(String... params) {




             aList = new ArrayList<HashMap<String, String>>();

             XMLParser parser = new XMLParser();

             String xml = parser.getXmlFromUrl(URL); // getting XML from URL


             Document doc = parser.getDomElement(xml);

             NodeList nl = doc.getElementsByTagName(KEY_SONG);

             // looping through all song nodes <song>

             try {
                for (int i = 0; i < nl.getLength(); i++) {
                    // creating new HashMap
                    map = new HashMap<String, String>();

                    Element e = (Element) nl.item(i);

                    // adding each child node to HashMap key => value
                    map.put(KEY_TEMPERATURE_MAXIMUM, parser.getValue(e, KEY_TEMPERATURE_MAXIMUM));

                    map.put(KEY_TEMPERATURE_MINIMUM, parser.getValue(e, KEY_TEMPERATURE_MINIMUM));

                    map.put(KEY_WEATHER_DESCRIPTION, parser.getValue(e, KEY_WEATHER_DESCRIPTION).trim());

                    map.put(KEY_PRECIPITATION, parser.getValue(e, KEY_PRECIPITATION));

                    map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));

                    // adding HashList to ArrayList
                    aList.add(map);

                }
            } catch (NullPointerException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

             adapter = new LazyAdapterForWeather(WeatherReportActivity.this, aList, 0);


            return "Executed";

         }      

         @Override
         protected void onPostExecute(String result) {
           if(mProgressDialog.isShowing()){
             mProgressDialog.dismiss();
         }
             list.setAdapter(adapter);

         }

         @Override
         protected void onPreExecute() {
         ShowLoading();
         }

         @Override
         protected void onProgressUpdate(Void... values) {

         }

     }

通过这种方式,您可以获得天气预报。用于定期在服务中运行相同的东西 你需要合并两个代码