android map封装json数据,android post带数据请求方式,传递的数据格式包括json和map...

如下:

public static String httpPost(String url, String json) {

try {

URL u = new URL(url);

HttpURLConnection httpURLConnection = (HttpURLConnection) u.openConnection();

httpURLConnection.setConnectTimeout(TIMEOUT);

httpURLConnection.setDoInput(true);

httpURLConnection.setDoOutput(true);

httpURLConnection.setRequestMethod("POST");

httpURLConnection.setUseCaches(false);

httpURLConnection.setRequestProperty("Content-Type",

"application/json");

httpURLConnection.setRequestProperty("Content-Length",

String.valueOf(json.getBytes().length));

OutputStream outputStream = httpURLConnection.getOutputStream();

outputStream.write(json.getBytes());

int response = httpURLConnection.getResponseCode();

if (response == HttpURLConnection.HTTP_OK) {

InputStream inptStream = httpURLConnection.getInputStream();

return dealResponseResult(inptStream);

}

} catch (Exception e) {

e.printStackTrace();

}

return "";

}

private static String dealResponseResult(InputStream inputStream) {

String resultData = null;

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

byte[] data = new byte[1024];

int len = 0;

try {

while ((len = inputStream.read(data)) != -1) {

byteArrayOutputStream.write(data, 0, len);

}

} catch (IOException e) {

e.printStackTrace();

}

resultData = new String(byteArrayOutputStream.toByteArray());

return resultData;

}

如果传的值不是json格式,而是map就可以采取下面这种格式

public static String httpPost(String url, Map params) {

byte[] data = getRequestData(params, "utf-8").toString().getBytes();

try {

URL u = new URL(url);

HttpURLConnection httpURLConnection = (HttpURLConnection) u.openConnection();

httpURLConnection.setConnectTimeout(TIMEOUT);

httpURLConnection.setDoInput(true);

httpURLConnection.setDoOutput(true);

httpURLConnection.setRequestMethod("POST");

httpURLConnection.setUseCaches(false);

httpURLConnection.setRequestProperty("Content-Type",

"application/x-www-form-urlencoded");

httpURLConnection.setRequestProperty("Content-Length",

String.valueOf(data.length));

OutputStream outputStream = httpURLConnection.getOutputStream();

outputStream.write(data);

int response = httpURLConnection.getResponseCode();

if (response == HttpURLConnection.HTTP_OK) {

InputStream inptStream = httpURLConnection.getInputStream();

return dealResponseResult(inptStream);

}

} catch (Exception e) {

e.printStackTrace();

}

return "";

}

private static StringBuffer getRequestData(Map params,

String encode) {

StringBuffer stringBuffer = new StringBuffer();

try {

for (Map.Entry entry : params.entrySet()) {

stringBuffer.append(entry.getKey())

.append("=")

.append(URLEncoder.encode(entry.getValue(), encode))

.append("&");

}

stringBuffer.deleteCharAt(stringBuffer.length() - 1);// remove the last "&"

} catch (Exception e) {

e.printStackTrace();

}

return stringBuffer;

}

原文:http://www.cnblogs.com/xiaoxiaing/p/5383688.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值