关于 Apache Http Client 的使用笔记

//添加http头信息
httppost.addHeader(Authorization, your token); //认证token
httppost.addHeader(Content-Type, application/json);
httppost.addHeader(User-Agent, imgfornote);
//http post的json数据格式:  {name: your name,parentId: id_of_parent}
JSONObject obj = new JSONObject();
obj.put(name, your name);
obj.put(parentId, your parentid);
httppost.setEntity(new StringEntity(obj.toString()));
HttpResponse response;
response = httpclient.execute(httppost);
//检验状态码,如果成功接收数据
int code = response.getStatusLine().getStatusCode();
if (code == 200) {
String rev = EntityUtils.toString(response.getEntity());//返回json格式: {id: 27JpL~j4vsL0LX00E00005,version: abc}
obj = new JSONObject(rev);
String id = obj.getString(id);
String version = obj.getString(version);}} catch (ClientProtocolException e) {
} catch (Exception e) {}}主要用到的类有:org.apache.http.client.HttpClient 、org.apache.http.client.methods.HttpPost 和org.json.JSONObject

猜你喜欢

转载自blog.csdn.net/qq_14861089/article/details/53319179
今日推荐