Java之~ http获取第三方数据

1.写一个http工具类demo

2.在接口或方法中调用

3.public class HttpDemo {

private String sendUrl = "要访问的URL路径";//短信
private CloseableHttpClient httpClient = null;

public HttpDemo(){
initHttpClient();
}

private void initHttpClient(){
httpClient = HttpClients.createMinimal();
}

/**

* @param recvMsisdn 接收者手机号码
* @param smsText  短信内容
*/
public String sendSmsByHttpClient(String recvMsisdn, String smsText){
String restr =null;
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();


params.add(new BasicNameValuePair("mobile", recvMsisdn));
params.add(new BasicNameValuePair("msgContent", smsText));

UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "UTF-8");
HttpPost postMethod = new HttpPost(sendUrl);
postMethod.setEntity(entity); //将参数填入POST Entity中  
HttpResponse response = httpClient.execute(postMethod); //执行POST方法  
int statuscode = response.getStatusLine().getStatusCode();
if(statuscode == 200){
restr = EntityUtils.toString(response.getEntity(), "UTF-8").trim();
}
} catch (Exception e) {
e.printStackTrace();

return restr;
}
private String docToString(Document doc){
StringWriter sw;
try {
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");
sw = new StringWriter();
XMLWriter writer = new XMLWriter(sw,format);
writer.write(doc.getRootElement());
writer.flush();
return sw.toString();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

private String sendUrlWeather = "URL";
//天气
public String sendWeatherByHttpClient(String recvMsisdn){
String restr =null;
try {
HttpGet getMethod = new HttpGet(sendUrlWeather+recvMsisdn);
HttpResponse response = httpClient.execute(getMethod); //执行get方法  
int statuscode = response.getStatusLine().getStatusCode();
if(statuscode == 200){
restr = EntityUtils.toString(response.getEntity(), "UTF-8").trim();
// System.out.println(restr);
}
} catch (Exception e) {
e.printStackTrace();

return restr;
}
//上传图片文件
public void createFaceToPerson(String personId,String img){
try {
HttpPost postMethod = new HttpPost(createFaceToPerson);
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();

File file = new File(img);
        //添加消息体信息         
multipartEntityBuilder.addBinaryBody("img", file);//文件
multipartEntityBuilder.addTextBody("personId", personId);//普通参数

HttpEntity entity = multipartEntityBuilder.build();
postMethod.setEntity(entity); //将参数填入POST Entity中  

HttpResponse response = httpClient.execute(postMethod); //执行POST方法  
int statuscode = response.getStatusLine().getStatusCode();
System.out.println(statuscode);
if(statuscode == 200){
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        StringBuffer buffer = new StringBuffer();
        String str = "";
        while(!StringUtils.isEmpty(str = reader.readLine())) {
            buffer.append(str);
        }
        System.out.println(buffer.toString());
}
} catch (Exception e) {
e.printStackTrace();

 

}

/**
* 物流

* @param com
*            公司名
* @param nu
*            快递单号
* @return
*/
public String sendwuliuUrlByHttpClient(String com, String nu) {
String restr = null;
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();


params.add(new BasicNameValuePair("com", com));
params.add(new BasicNameValuePair("nu", nu));


UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "UTF-8");
HttpPost postMethod = new HttpPost(wuliuUrl);
postMethod.setEntity(entity); // 将参数填入POST Entity中
HttpResponse response = httpClient.execute(postMethod); // 执行POST方法
int statuscode = response.getStatusLine().getStatusCode();
if (statuscode == 200) {
restr = EntityUtils.toString(response.getEntity()).trim();
restr = new String(restr.getBytes("ISO-8859-1"), "GBK");//乱码的要转一下
}
} catch (Exception e) {
e.printStackTrace();
}
return restr;
}

4.调用方法,用gson解析

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值