好凌乱的题目,只是一些功能点的总结咯。
首先构造一个json对象用于存放数据,如果光加上header为utf-8就能解决中文就大错特错了。。。
json对象可以put变量,也可以put对象。取的时候obj.getJSONObject("people").getString("name")
- HttpClient httpClient = new DefaultHttpClient();
- String url = "***";
- HttpPost httpPost = new HttpPost(url);
- HttpResponse response;
- httpPost.addHeader("Content-Type", "application/json;charset=utf-8");
- JSONObject obj = new JSONObject();
- obj.put("action", "postHttp");
- JSONObject people= new JSONObject();
- people.put("name", "张三");
- obj.put("people",people);
接下来就是处理中文了。这么看的话好像没啥难的哈,就是把obj在转成StringEntity的时候限制一个格式再发给httpPost。
- StringEntity se = new StringEntity( obj.toString(),"UTF-8");
- se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
- httpPost.setEntity(se);
- response = httpClient.execute(httpPost);
- int code = response.getStatusLine().getStatusCode();
- 。。。。。。
- 顶
- 0
- 踩