Reputation: 1
Hi everyone i just want to ask, how will i be able to notify my phone if its gonna rain like when you send a request for the current weather the request should run on background and if its gonna rain then it will notify.
Can anyone give some hints or any tutorial for me to able to send a request through a service and notify me if its gonna rain please share some ideas
cause i really need it and thank you.
i have done a class for requesting the current location and i use openweathermap API for me to get the current weather.
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;
}
}
Upvotes: 0
Views: 407
Reputation: 217
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) {
}
}
See in this way you can get the weather report . For running the same thing in service at regular interval you need to merge two code
Upvotes: 1