联系客服: iP138.com客服 iP138客服 |

天气预报接口
北京市天气实况

3℃ 晴

风向风速:西北风2级

相对湿度:27%

空气质量:27优

  • API接口QQ交流群:177096428   iP138 api接口
  • 数据修正QQ处理群:94181690    iP138数据修正

天气预报查询接口支持HTTPS(赠送1000次)

简介:获取全国今天天气及最近7天的天气预报情况

已连接应用数:1679

Java调用天气查询接口示例:

                                    public class QueryHelper {

                                        /**
                                         * txt|jsonp|xml
                                         */
                                        public static String DATATYPE="text";
                                        
                                        public static String get(String urlString,String token) {
                                            try {
                                                URL url = new URL(urlString);
                                                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                                                conn.setConnectTimeout(5 * 1000);
                                                conn.setReadTimeout(5 * 1000);
                                                conn.setDoInput(true);
                                                conn.setDoOutput(true);
                                                conn.setUseCaches(false);
                                                conn.setInstanceFollowRedirects(false);
                                                conn.setRequestMethod("GET"); 
                                                conn.setRequestProperty("token",token);
                                                int responseCode = conn.getResponseCode();
                                                if (responseCode == 200) {
                                                    StringBuilder builder = new StringBuilder();
                                                    BufferedReader br = new BufferedReader(
                                                            new InputStreamReader(conn.getInputStream(),"utf-8"));
                                                    for (String s = br.readLine(); s != null; s = br
                                                            .readLine()) {
                                                        builder.append(s);
                                                    }
                                                    br.close();
                                                    return builder.toString();
                                                }
                                            } catch (IOException e) {
                                                e.printStackTrace();
                                            }
                                            return null;
                                        }
                                        public static String queryWeather(String code){
                                            String url="https://api.ip138.com/weather/="+code;
                                            String token="c1dd42049a489b73a39d4fcca47454f4";
                                            return get(url,token);
                                        }

                                    }

                                    //以下是使用示例:
                                    //QueryHelper.queryWeather("350103")