tag 标签: 天气

相关博文
  • 热度 22
    2015-12-3 21:49
    2224 次阅读|
    1 个评论
    安卓从百度天气接口获取天气,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\": ,\"weather_data\": }]}";         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%ACoutput=jsonak=");                    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();     }    
  • 热度 20
    2015-11-30 22:26
    1047 次阅读|
    0 个评论
      今天问题还真是挺多的,对于当中台架出现的问题自己只能一步步的去排查。   回到住的地方跑了下步,今天上海的天气PM值很低,其次有可能有一个情况就是明天有可能要变冷了。
  • 热度 26
    2013-7-25 06:34
    1166 次阅读|
    2 个评论
    连续一个星期都是晴天,中午空旷地最少40度,神马天气预报还说36度,超搞笑啊!
  • 热度 13
    2012-12-13 08:43
    811 次阅读|
    0 个评论
    哈哈 今天天气不错 真好
  • 热度 11
    2012-7-3 14:55
    1196 次阅读|
    1 个评论
      这几天天气不错,蓝天白云。因为上周下了大雨,所以空气也显得比较干净。  
相关资源