android通过gson发送json格式的httppost请求,http发送json格式数据请求 demo示例

http发送json格式数据请求 demo示例

import com.google.gson.JsonArray;

import com.google.gson.JsonObject;

import org.apache.http.HttpResponse;

import org.apache.http.HttpStatus;

import org.apache.http.client.HttpClient;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.entity.StringEntity;

import org.apache.http.impl.client.DefaultHttpClient;

import org.apache.http.util.EntityUtils;

import java.io.IOException;

import java.nio.charset.Charset;

/**

* Created by admin on 2016/8/3.

*/

public class JsonHttpClient {

// 接口地址

private static String apiURL = "http://localhost:8080/...";

private HttpClient httpClient = null;

private HttpPost method = null;

private long startTime = 0L;

private long endTime = 0L;

private int status = 0;

/**

* 接口地址

*

* @param url

*/

public JsonHttpClient(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;

if (method != null & parameters != null

&& !"".equals(parameters.trim())) {

try {

// 建立一个NameValuePair数组,用于存储欲传送的参数

method.addHeader("Content-type","application/json; charset=utf-8");

method.setHeader("Accept", "application/json");

method.setEntity(new StringEntity(parameters, Charset.forName("UTF-8")));

startTime = System.currentTimeMillis();

HttpResponse response = httpClient.execute(method);

endTime = System.currentTimeMillis();

int statusCode = response.getStatusLine().getStatusCode();

if (statusCode != HttpStatus.SC_OK) {

status = 1;

}

// Read the response body

body = EntityUtils.toString(response.getEntity());

} catch (IOException e) {

// 网络错误

status = 3;

}

}

return body;

}

public static void main(String[] args) {

JsonHttpClient ac = new JsonHttpClient(apiURL);

//创建参数列表

JsonArray array = new JsonArray();

JsonObject object = new JsonObject();

object.addProperty("aa","123");

object.addProperty("bb","218");

array.add(object);

JsonObject object1 = new JsonObject();

object1.addProperty("aa","456");

object1.addProperty("bb","2.2");

array.add(object1);

JsonObject res = new JsonObject();

res.addProperty("list",array.toString());

res.addProperty("mm","mmVal");

System.out.println(ac.post(res.toString()));

}

/**

* 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;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值