7 changed files with 263 additions and 19 deletions
@ -0,0 +1,22 @@ |
|||
package com.ccsens.logistics.bean.dto; |
|||
|
|||
import io.swagger.annotations.ApiModel; |
|||
import io.swagger.annotations.ApiModelProperty; |
|||
import lombok.Data; |
|||
|
|||
/** |
|||
* @author 马 |
|||
*/ |
|||
@Data |
|||
public class HttpDto { |
|||
|
|||
@Data |
|||
@ApiModel("温湿度登录接口") |
|||
public static class Login{ |
|||
@ApiModelProperty("账号") |
|||
private String loginName; |
|||
@ApiModelProperty("密码") |
|||
private String password; |
|||
} |
|||
|
|||
} |
@ -0,0 +1,106 @@ |
|||
package com.ccsens.logistics.service; |
|||
|
|||
import com.alibaba.fastjson.JSON; |
|||
import com.alibaba.fastjson.JSONObject; |
|||
import com.ccsens.logistics.bean.dto.HttpDto; |
|||
import com.ccsens.util.CodeEnum; |
|||
import com.ccsens.util.JsonResponse; |
|||
import com.ccsens.util.PropUtil; |
|||
import com.ccsens.util.RestTemplateUtil; |
|||
import com.ccsens.util.exception.BaseException; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.apache.http.Header; |
|||
import org.apache.http.HttpResponse; |
|||
import org.apache.http.client.ClientProtocolException; |
|||
import org.apache.http.client.HttpClient; |
|||
import org.apache.http.client.methods.HttpGet; |
|||
import org.apache.http.impl.client.CloseableHttpClient; |
|||
import org.apache.http.impl.client.HttpClients; |
|||
import org.apache.http.util.EntityUtils; |
|||
import org.apache.http.HttpEntity; |
|||
import org.springframework.http.ResponseEntity; |
|||
import org.springframework.stereotype.Service; |
|||
import org.springframework.transaction.annotation.Propagation; |
|||
import org.springframework.transaction.annotation.Transactional; |
|||
|
|||
import java.io.IOException; |
|||
import java.net.URI; |
|||
import java.net.URISyntaxException; |
|||
import java.util.Arrays; |
|||
import java.util.Map; |
|||
|
|||
/** |
|||
* @author 马 |
|||
*/ |
|||
@Slf4j |
|||
@Service |
|||
@Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class) |
|||
public class HttpService implements IHttpService{ |
|||
|
|||
@Override |
|||
public void getTemperatureAndHumidity() { |
|||
try { |
|||
HttpDto.Login login = new HttpDto.Login(); |
|||
login.setLoginName("jnrstest"); |
|||
login.setPassword("jnrstest321"); |
|||
//调取登录接口 获取userId
|
|||
String userId = getUserId(login); |
|||
String body = getBody(userId); |
|||
|
|||
}catch (Exception e){ |
|||
e.printStackTrace(); |
|||
} |
|||
|
|||
} |
|||
|
|||
/** |
|||
* 温湿度登录接口 获取设备的userId 调用其他接口 |
|||
* @param login 账号密码 |
|||
* @return userId |
|||
*/ |
|||
public String getUserId(HttpDto.Login login){ |
|||
String requestUrl = "http://www.0531yun.cn/wsjc/app/Login"; |
|||
String s = RestTemplateUtil.postBody(requestUrl, login); |
|||
JsonResponse<JSONObject> a = JSONObject.parseObject(s, JsonResponse.class); |
|||
JSONObject data = a.getData(); |
|||
String userId = data.getString("userId"); |
|||
return userId; |
|||
} |
|||
|
|||
|
|||
|
|||
public static String getBody(String userId) throws IOException { |
|||
try { |
|||
String url = "http://www.0531yun.cn/app/GetUserDeviceGroups"; |
|||
HttpGet get = new HttpGet(url); |
|||
get.setHeader("userId",userId); |
|||
HttpClient httpClient = HttpClients.createDefault(); |
|||
HttpResponse response = httpClient.execute(get); |
|||
return getResult(response); |
|||
} catch (ClientProtocolException e) { |
|||
e.printStackTrace(); |
|||
} catch (IOException e) { |
|||
e.printStackTrace(); |
|||
} |
|||
return null; |
|||
} |
|||
|
|||
public static String getResult(HttpResponse response) throws IOException { |
|||
// 获取状态码
|
|||
int code = response.getStatusLine().getStatusCode(); |
|||
System.out.println(code); |
|||
// 获取body
|
|||
HttpEntity entity = response.getEntity(); |
|||
String body = EntityUtils.toString(entity); |
|||
System.out.println(body); |
|||
// 获取头信息
|
|||
Header[] allHeaders = response.getAllHeaders(); |
|||
String headers = Arrays.toString(allHeaders); |
|||
System.out.println(headers); |
|||
|
|||
// 返回body
|
|||
return body; |
|||
} |
|||
|
|||
|
|||
} |
@ -0,0 +1,13 @@ |
|||
package com.ccsens.logistics.service; |
|||
|
|||
/** |
|||
* @author 马 |
|||
*/ |
|||
public interface IHttpService { |
|||
|
|||
/** |
|||
* 获取温度和湿度 |
|||
*/ |
|||
void getTemperatureAndHumidity(); |
|||
|
|||
} |
Loading…
Reference in new issue