JSON

使用Apache HTTPClient,并以JSON为参数进行POST请求

字号+ 作者:H5之家 来源:H5之家 2015-11-03 11:20 我要评论( )

依赖JSON、HTTPClient等jar包

代码片段(1) [全屏查看所有代码]

1. [代码]JSON,HTTP POST     跳至 [ 12c8 屏预览] package org.ssi.util; import java.io.IOException; import java.util.ArrayList; import java.util.List; import net.sf.json.JSONArray; import org.apache.commons.lang.exception.ExceptionUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.protocol.HTTP; import org.apache.http.util.EntityUtils; public class APIHttpClient { //接口地址 private String apiURL = ""; private Log logger = LogFactory.getLog(this.getClass()); private static final String pattern = "yyyy-MM-dd HH:mm:ss:SSS"; private HttpClient httpClient = null; private HttpPost method = null; private long startTime = 0L; private long endTime = 0L; private int status = 0; /** * 接口地址 * @param url */ public APIHttpClient(String url){ if(url != null) { this.apiURL = url; } if(apiURL != null) { httpClient = new DefaultHttpClient(); method = new HttpPost(apiURL); } } /** * 调用 API * @param parameters * @return */ public String post(String parameters) { String body = null; logger.info("parameters:" + parameters); if(method != null & parameters != null && !"".equals(parameters.trim())) { JSONArray jsonObject = JSONArray.fromObject(parameters); logger.info("json:" + jsonObject.toString()); try{ List<NameValuePair> params=new ArrayList<NameValuePair>(); //建立一个NameValuePair数组,用于存储欲传送的参数 params.add(new BasicNameValuePair("data",parameters)); //添加参数 method.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8)); startTime = System.currentTimeMillis(); //设置编码 HttpResponse response=httpClient.execute(method); endTime = System.currentTimeMillis(); int statusCode = response.getStatusLine().getStatusCode(); logger.info("statusCode:" + statusCode); logger.info("调用API 花费时间(单位:毫秒):" + (endTime - startTime)); if(statusCode != HttpStatus.SC_OK){ logger.error("Method failed:"+response.getStatusLine()); status = 1; } //Read the response body body=EntityUtils.toString(response.getEntity()); }catch(IOException e){ //发生网络异常 logger.error("exception occurred!\n"+ExceptionUtils.getFullStackTrace(e)); //网络错误 status = 3; } finally{ logger.info("调用接口状态:" + status); } } return body; } /** * 0.成功 1.执行方法失败 2.协议错误 3.网络错误 * @return the status */ public int getStatus() { return status; } /** * @param status the status to set */ public void setStatus(int status) { this.status = status; } /** * @return the startTime */ public long getStartTime() { return startTime; } /** * @return the endTime */ public long getEndTime() { return endTime; } }

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • 浅谈使用PHP开发微信支付的流程

    浅谈使用PHP开发微信支付的流程

    2016-02-13 15:00

  • Unity3D脚本:Unity3D使用LitJson解析服务器上的JSON

    Unity3D脚本:Unity3D使用LitJson解析服务器上的JSON

    2016-01-31 10:32

  • HttpClient 4.3教程 第一章 基本概念

    HttpClient 4.3教程 第一章 基本概念

    2016-01-26 16:49

  • WebServices中使用JSON

    WebServices中使用JSON

    2016-01-24 18:13

网友点评