Java calls China sky network API to obtain the weather forecast information method

  • 2020-04-01 04:01:52
  • OfStack

In this paper, an example is given to illustrate how Java can obtain weather forecast information by calling the API of China weather network. Share with you for your reference. The specific implementation method is as follows:


//Take the ice city of Harbin as an example to call the weather information through the Chinese weather API
private String getWeatherInfo2(){
  StringBuilder info = new StringBuilder();
  try {
   DefaultHttpClient httpclient = new DefaultHttpClient();
   HttpGet httget = new HttpGet("http://m.weather.com.cn/data/101050101.html");
   ResponseHandler<String> responseHandler = new BasicResponseHandler();
   String responseBody = httpclient.execute(httget, responseHandler);
   System.out.println(responseBody);
   JsonParser jp = new JsonParser();
   JsonElement jse = jp.parse(responseBody);
   JsonObject jso = jse.getAsJsonObject().get("weatherinfo").getAsJsonObject();
//   String updTime = jso.get("fchh").getAsString();
//   if(updTime != null){
//    //The temperature
//    String j = jso.get("temp1").getAsString();//Today,
//    String m = jso.get("temp2").getAsString();//Tomorrow,
//    //The weather in
//    String j_weather = jso.get("weather1").getAsString();//Today,
//    String m_weather = jso.get("weather2").getAsString();//Tomorrow,
//    //The wind wind
//    String j_wind = jso.get("wind1").getAsString();//Today,
//    String m_wind = jso.get("wind2").getAsString();//Tomorrow,
//Info. Append (" today: "), append (j) append (" "), append (j_weather). Append (" "), append (j_wind). Append ("  n ");
//Info. Append (" tomorrow. "). Append (m), append (" "), append (m_weather). Append (" "), append (m_wind). Append ("  n ");
//   }
   String updTime = jso.get("fchh").getAsString();
   if(updTime != null){
    if(!updTime.trim().equals("18")){
     //The temperature
     String j = jso.get("temp1").getAsString();//Today,
     String m = jso.get("temp2").getAsString();//Tomorrow,
     //The weather in
     String j_weather = jso.get("weather1").getAsString();//Today,
     String m_weather = jso.get("weather2").getAsString();//Tomorrow,
     //The wind wind
     String j_wind = jso.get("wind1").getAsString();//Today,
     String m_wind = jso.get("wind2").getAsString();//Tomorrow,
 Info. Append (" today: "), append (j) append (" "), append (j_weather). Append (" "), append (j_wind). Append ("  n ");
 Info. Append (" tomorrow. "). Append (m), append (" "), append (m_weather). Append (" "), append (m_wind). Append ("  n ");
    }else{
     //18
     //The temperature
     String temp1 = jso.get("temp1").getAsString();//Today,
     String temp2 = jso.get("temp2").getAsString();//Today,
     String temp3 = jso.get("temp3").getAsString();//Today,
     String j = temp1.split("~")[1] + "~" + temp2.split("~")[0];
     String m = temp2.split("~")[1] + "~" + temp3.split("~")[0];//Tomorrow,
     //The weather in
     String weather1 = jso.get("weather1").getAsString();
     String weather2 = jso.get("weather2").getAsString();
     String weather3 = jso.get("weather3").getAsString();
     String j_weather = "";
     String j_weather_part1 = "";
     String j_weather_part2 = "";
     //Determine whether there is a rotation
     if(weather1.indexOf(" turn ") > 0){
      //There are
      j_weather_part1 = weather1.split(" turn ")[1];
     }else{
      j_weather_part1 = weather1;
     }
     if(weather2.indexOf(" turn ") > 0){
      //There are
      j_weather_part2 = weather2.split(" turn ")[0];
     }else{
      j_weather_part2 = weather2;
     }
     if(j_weather_part1.equalsIgnoreCase(j_weather_part2)){
      j_weather = j_weather_part1;//Today,
     }else{
      j_weather = j_weather_part1 + " turn " + j_weather_part2;//Today,
     }
     String m_weather = "";
     String m_weather_part1 = "";
     String m_weather_part2 = "";
     //Determine whether there is a rotation
     if(weather2.indexOf(" turn ") > 0){
      //There are
      m_weather_part1 = weather2.split(" turn ")[1];
     }else{
      m_weather_part1 = weather2;
     }
     if(weather3.indexOf(" turn ") > 0){
      //There are
      m_weather_part2 = weather3.split(" turn ")[0];
     }else{
      m_weather_part2 = weather3;
     }
     if(m_weather_part1.equalsIgnoreCase(m_weather_part2)){
      m_weather = m_weather_part1;//Today,
     }else{
      m_weather = m_weather_part1 + " turn " + m_weather_part2;//Tomorrow,
     }
     //The wind wind
     String j_wind = jso.get("wind2").getAsString();//Today,
     String m_wind = jso.get("wind3").getAsString();//Tomorrow,
 Info. Append (" today: "), append (j) append (" "), append (j_weather). Append (" "), append (j_wind). Append ("  n ");
 Info. Append (" tomorrow. "). Append (m), append (" "), append (m_weather). Append (" "), append (m_wind). Append ("  n ");
    }
   }
  } catch (Exception e) {
  }
  return info.toString();
}

I hope this article has been helpful to your Java programming.


Related articles: