首页

微信小程序天气(含java后端)

java

2020-6-17


package com.leadingsoft.liuw.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by liuw on 2017/4/19.
 */
@Slf4j
@RestController
@RequestMapping("/w/weather")
public class WeatherController {

    @RequestMapping(method = RequestMethod.GET)
    public String get(){
        String result = "";
        try {
            URL url = new URL("http://api.openweathermap.org/data/2.5/weather?q=Dalian&appid=dafef1a9be486b5015eb92330a0add7d");
            HttpURLConnection connection = (HttpURLConnection)url.openConnection();
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setRequestMethod("GET");
            connection.setUseCaches(false);
            connection.setInstanceFollowRedirects(true);
            connection.connect();
            InputStream in = connection.getInputStream();

            BufferedReader read = new BufferedReader(new InputStreamReader(in, "UTF-8"));

            String line="";
            int i=0;
            while ( (line=read.readLine())!= null ) {
                result  = line;
                i  ;
            }
            // 断开连接
            read.close();
            in.close();
            connection.disconnect();
        } catch (Exception e) {
            WeatherController.log.error("取得天气数据失败", e);
        }

        return result;
    }
}

资源下载此资源下载价格为2D币(VIP免费),请先
资源文件列表
微信小程序天气(含java后端)/142826fj4p4g4qq7scn9ps.jpg , 10934
微信小程序天气(含java后端)/【前端】WxMeetingWeixinend-master.zip , 56873
微信小程序天气(含java后端)/【后端】WxMeetingBackend-master.zip , 24702
没有账号? 忘记密码?

社交账号快速登录