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解析