用httpPost对JSON发送和接收的例子
来源:互联网 发布:树莓派gpio编程c语言 编辑:程序博客网 时间:2024/04/28 11:11
HTTPPost发送JSON:
private static final String APPLICATION_JSON = "application/json";
private static final String CONTENT_TYPE_TEXT_JSON = "text/json";
public static void httpPostWithJSON(String url, String json) throws Exception {
// 将JSON进行UTF-8编码,以便传输中文
String encoderJson = URLEncoder.encode(json, HTTP.UTF_8);
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader(HTTP.CONTENT_TYPE, APPLICATION_JSON);
StringEntity se = new StringEntity(encoderJson);
se.setContentType(CONTENT_TYPE_TEXT_JSON);
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, APPLICATION_JSON));
httpPost.setEntity(se);
httpClient.execute(httpPost);
}
private static final String CONTENT_TYPE_TEXT_JSON = "text/json";
public static void httpPostWithJSON(String url, String json) throws Exception {
// 将JSON进行UTF-8编码,以便传输中文
String encoderJson = URLEncoder.encode(json, HTTP.UTF_8);
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader(HTTP.CONTENT_TYPE, APPLICATION_JSON);
StringEntity se = new StringEntity(encoderJson);
se.setContentType(CONTENT_TYPE_TEXT_JSON);
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, APPLICATION_JSON));
httpPost.setEntity(se);
httpClient.execute(httpPost);
}
接收HTTPPost中的JSON:
public static String receivePost(HttpServletRequest request) throws IOException, UnsupportedEncodingException {
// 读取请求内容
BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
String line = null;
StringBuilder sb = new StringBuilder();
while((line = br.readLine())!=null){
sb.append(line);
}
// 将资料解码
String reqBody = sb.toString();
return URLDecoder.decode(reqBody, HTTP.UTF_8);
}
// 读取请求内容
BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
String line = null;
StringBuilder sb = new StringBuilder();
while((line = br.readLine())!=null){
sb.append(line);
}
// 将资料解码
String reqBody = sb.toString();
return URLDecoder.decode(reqBody, HTTP.UTF_8);
}
0 0
- 用httpPost对JSON发送和接收的例子
- 用httpPost对JSON发送和接收的例子
- 用httpPost对JSON发送和接收的例子
- 用httpPost对JSON发送和接收的例子
- 用httpPost对JSON发送和接收的例子
- 用httpPost对JSON发送和接收的例子
- 用httpPost对Json发送和接收的例子
- 用httpPost对JSON发送和接收的例子
- 用httpPost对JSON发送和接收的例子
- 用httpPost对JSON发送和接收的例子
- 用httpPost对JSON发送和接收的例子
- httpPost对JSON发送和接收
- (转载).Net HttpPost的发送和接收示例代码
- json的发送和接收
- HttpPost发送Json
- HttpPost发送Json
- HTTPPOST 发送JSON格式参数
- MPI一个简单的发送和接收的例子
- Volley的初级使用(从网络下载图片)
- Bug3 ---error C2039: “SetWindowTextA”: 不是“ATL::CStringT<BaseType,StringTraits>”的成员
- Meun菜单选项
- Menu简单的菜单选项
- 牛客网读书笔记
- 用httpPost对JSON发送和接收的例子
- 程序员的书写格式要求t
- AndroidStudio插件GsonFormat快速实现Java实体对象
- Machine Learning into Action NOTE 00
- 问题跟踪管理软件工具(JIRI)
- ubuntu 14.04 使用 p4merge
- 图的深搜与广搜
- MFC 之双缓冲
- 用GO帮朋友写了一个批量群发的软件,其中一个需求是使用rsa登录ssh,代码片段分享.