读书人

用httpPost对JSON发送跟接收的例子

发布时间: 2013-10-27 15:21:49 作者: rapoo

用httpPost对JSON发送和接收的例子
private?static?final?String?APPLICATION_JSON?=?"application/json";
用httpPost对JSON发送跟接收的例子????
用httpPost对JSON发送跟接收的例子????private?static?final?String?CONTENT_TYPE_TEXT_JSON?=?"text/json";
用httpPost对JSON发送跟接收的例子
用httpPost对JSON发送跟接收的例子public?static?void?httpPostWithJSON(String?url,?String?json)?throws?Exception?{
用httpPost对JSON发送跟接收的例子????????//?将JSON进行UTF-8编码,以便传输中文
用httpPost对JSON发送跟接收的例子????????String?encoderJson?=?URLEncoder.encode(json,?HTTP.UTF_8);
用httpPost对JSON发送跟接收的例子????????
用httpPost对JSON发送跟接收的例子????????DefaultHttpClient?httpClient?=?new?DefaultHttpClient();
用httpPost对JSON发送跟接收的例子????????HttpPost?httpPost?=?new?HttpPost(url);
用httpPost对JSON发送跟接收的例子????????httpPost.addHeader(HTTP.CONTENT_TYPE,?APPLICATION_JSON);
用httpPost对JSON发送跟接收的例子????????
用httpPost对JSON发送跟接收的例子????????StringEntity?se?=?new?StringEntity(encoderJson);
用httpPost对JSON发送跟接收的例子????????se.setContentType(CONTENT_TYPE_TEXT_JSON);
用httpPost对JSON发送跟接收的例子????????se.setContentEncoding(new?BasicHeader(HTTP.CONTENT_TYPE,?APPLICATION_JSON));
用httpPost对JSON发送跟接收的例子????????httpPost.setEntity(se);
用httpPost对JSON发送跟接收的例子????????httpClient.execute(httpPost);
用httpPost对JSON发送跟接收的例子????}


接收HTTPPost中的JSON:

用httpPost对JSON发送跟接收的例子public?static?String?receivePost(HttpServletRequest?request)?throws?IOException,?UnsupportedEncodingException?{
用httpPost对JSON发送跟接收的例子????????
用httpPost对JSON发送跟接收的例子????????//?读取请求内容
用httpPost对JSON发送跟接收的例子????????BufferedReader?br?=?new?BufferedReader(new?InputStreamReader(request.getInputStream()));
用httpPost对JSON发送跟接收的例子????????String?line?=?null;
用httpPost对JSON发送跟接收的例子????????StringBuilder?sb?=?new?StringBuilder();
用httpPost对JSON发送跟接收的例子????????while((line?=?br.readLine())!=null){
用httpPost对JSON发送跟接收的例子????????????sb.append(line);
用httpPost对JSON发送跟接收的例子????????}
用httpPost对JSON发送跟接收的例子
用httpPost对JSON发送跟接收的例子????????//?将资料解码
用httpPost对JSON发送跟接收的例子????????String?reqBody?=?sb.toString();
用httpPost对JSON发送跟接收的例子????????return?URLDecoder.decode(reqBody,?HTTP.UTF_8);
用httpPost对JSON发送跟接收的例子????}转:http://www.blogjava.net/duansky/archive/2012/03/18/372137.html

读书人网 >操作系统

热点推荐