安卓从百度天气接口获取天气,Http和JSON操作
wxleasyland@sina.com
2015.12
url中的ak=后面要加上百度的key,请自行申请。
关键代码如下,比较简单:
import org.json.JSONArray;
import org.json.JSONObject;
import android.os.Handler;
import java.net.URL;
import java.net.HttpURLConnection;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
// 百度天气接口JSON示例 String g_str="{\"error\":0,\"status\":\"success\",\"date\":\"2015-11-17\",\"results\":[{\"currentCity\":\"北京\",\"pm25\":\"77\",\"index\":[{\"title\":\"穿衣\",\"zs\":\"较冷\",\"tipt\":\"穿衣指数\",\"des\":\"建议着厚外套加毛衣等服装。年老体弱者宜着大衣、呢外套加羊毛衫。\"},{\"title\":\"洗车\",\"zs\":\"不宜\",\"tipt\":\"洗车指数\",\"des\":\"不宜洗车,未来24小时内有雨,如果在此期间洗车,雨水和路上的泥水可能会再次弄脏您的爱车。\"},{\"title\":\"旅游\",\"zs\":\"适宜\",\"tipt\":\"旅游指数\",\"des\":\"天气较好,但丝毫不会影响您出行的心情。温度适宜又有微风相伴,适宜旅游。\"},{\"title\":\"感冒\",\"zs\":\"少发\",\"tipt\":\"感冒指数\",\"des\":\"各项气象条件适宜,无明显降温过程,发生感冒机率较低。\"},{\"title\":\"运动\",\"zs\":\"较不宜\",\"tipt\":\"运动指数\",\"des\":\"天气较好,但考虑天气寒冷,推荐您进行室内运动,户外运动时请注意保暖并做好准备活动。\"},{\"title\":\"紫外线强度\",\"zs\":\"最弱\",\"tipt\":\"紫外线强度指数\",\"des\":\"属弱紫外线辐射天气,无需特别防护。若长期在户外,建议涂擦SPF在8-12之间的防晒护肤品。\"}],\"weather_data\":[{\"date\":\"周二 11月17日 (实时:7℃)\",\"dayPictureUrl\":\"http://api.map.baidu.com/images/weather/day/duoyun.png\",\"nightPictureUrl\":\"http://api.map.baidu.com/images/weather/night/yujiaxue.png\",\"weather\":\"多云转雨夹雪\",\"wind\":\"微风\",\"temperature\":\"11 ~ 2℃\"},{\"date\":\"周三\",\"dayPictureUrl\":\"http://api.map.baidu.com/images/weather/day/yin.png\",\"nightPictureUrl\":\"http://api.map.baidu.com/images/weather/night/yin.png\",\"weather\":\"阴\",\"wind\":\"微风\",\"temperature\":\"5 ~ 3℃\"},{\"date\":\"周四\",\"dayPictureUrl\":\"http://api.map.baidu.com/images/weather/day/yujiaxue.png\",\"nightPictureUrl\":\"http://api.map.baidu.com/images/weather/night/yujiaxue.png\",\"weather\":\"雨夹雪\",\"wind\":\"微风\",\"temperature\":\"5 ~ 2℃\"},{\"date\":\"周五\",\"dayPictureUrl\":\"http://api.map.baidu.com/images/weather/day/yujiaxue.png\",\"nightPictureUrl\":\"http://api.map.baidu.com/images/weather/night/xiaoyu.png\",\"weather\":\"雨夹雪转小雨\",\"wind\":\"微风\",\"temperature\":\"4 ~ 1℃\"}]}]}";
void mygeneratestr() //启动一个线程,进行http连接,整理天气数据
{
new Thread(new Runnable()
{
@Override
public void run()
{
g_str="";
g_str2="";
g_str3="";
g_str4="";
String str="";
HttpURLConnection connection=null;
try
{
URL url=new URL("http://api.map.baidu.com/telematics/v3/weather?location=%E5%8C%97%E4%BA%AC&output=json&ak=");
connection =(HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(8000);
connection.setReadTimeout(8000);
InputStream in=connection.getInputStream();
//下面对获取到的输入流进行读取
BufferedReader reader=new BufferedReader(new InputStreamReader(in));
StringBuilder sb=new StringBuilder();
while((str=reader.readLine())!=null) sb.append(str);
g_str+=sb.toString();
}
catch(Exception e)
{
//e.printStackTrace();
g_str=null;
g_str2="http出错。错误信息:"+e.toString();
}
finally
{
if(connection!=null) connection.disconnect();
}
if(g_str==null) { g_str=""; return;}
if(!g_str.contains("\"status\":\"success\""))
{
g_str2="http得到的信息不符:"+g_str;
g_str="";
return;
}
try
{
JSONObject myobj=new JSONObject(g_str); //总OBJECT
JSONObject myobj2=myobj.getJSONArray("results").getJSONObject(0);
JSONObject myobj31=myobj2.getJSONArray("index").getJSONObject(0);//穿衣
JSONObject myobj32=myobj2.getJSONArray("index").getJSONObject(1);//洗车
JSONObject myobj33=myobj2.getJSONArray("index").getJSONObject(2);//旅游
JSONObject myobj34=myobj2.getJSONArray("index").getJSONObject(3);//感冒
JSONObject myobj35=myobj2.getJSONArray("index").getJSONObject(4);//运动
JSONObject myobj36=myobj2.getJSONArray("index").getJSONObject(5);//紫外线
JSONObject myobj41=myobj2.getJSONArray("weather_data").getJSONObject(0);//今
JSONObject myobj42=myobj2.getJSONArray("weather_data").getJSONObject(1);//明
JSONObject myobj43=myobj2.getJSONArray("weather_data").getJSONObject(2);//后
JSONObject myobj44=myobj2.getJSONArray("weather_data").getJSONObject(3);//大后
g_str2=myobj2.getString("currentCity")+myobj41.getString("date")+"指数"+myobj2.getString("pm25")+"\n"
+"今天 "+myobj41.getString("temperature")+" "+myobj41.getString("weather")+" "+myobj41.getString("wind")+"\n"
+"明天 "+myobj42.getString("temperature")+" "+myobj42.getString("weather")+" "+myobj42.getString("wind")+"\n"
+"后天 "+myobj43.getString("temperature")+" "+myobj43.getString("weather")+" "+myobj43.getString("wind")+"\n"
+"大后 "+myobj44.getString("temperature")+" "+myobj44.getString("weather")+" "+myobj44.getString("wind")+"\n"
+myobj31.getString("des");
//g_str3=myobj31.getString("des")+"\n"
// +myobj34.getString("des")+"\n"
// +myobj36.getString("des");
g_str4="今"+myobj41.getString("temperature")+"\n"
+"明"+myobj42.getString("temperature");
}
catch (Exception e)
{
g_str="";
g_str2 ="http正常,但JSON 出错";
g_str3 ="";
g_str4 ="";
//e.printStackTrace();
}
}
}).start();
}
用户593939 2015-12-7 22:23