0

大家好,我只是想问一下,如果它会下雨,我将如何通知我的手机,就像您发送当前天气的请求时,该请求应该在后台运行,如果它会下雨,那么它会通知。

谁能给我一些提示或任何教程让我能够通过服务发送请求并通知我如果下雨请分享一些想法

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

我已经完成了一个请求当前位置的类,我使用 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;
    }


}
4

1 回答 1

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) {

         }

     }

看看这样就可以得到天气预报了。为了定期在服务中运行相同的东西,您需要合并两个代码

于 2013-08-01T06:56:46.370 回答