大家好,我只是想问一下,如果它会下雨,我将如何通知我的手机,就像您发送当前天气的请求时,该请求应该在后台运行,如果它会下雨,那么它会通知。
谁能给我一些提示或任何教程让我能够通过服务发送请求并通知我如果下雨请分享一些想法
因为我真的需要它,谢谢。
我已经完成了一个请求当前位置的类,我使用 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;
}
}