httppost加json解析
package com.java1234.http;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* 请求注册user
* @author 佟印龙
*
*/
public class CeSHI {
public static void main(String[] args) {
CeSHI c=new CeSHI();
c.requestByPostMethod();
}
public void requestByPostMethod(){
CloseableHttpClient httpClient = getHttpClient();
try {
HttpPost post = new HttpPost("http://cloudapi.dezhi.com/weike");
//创建参数列表
List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
list.add(new BasicNameValuePair("commandid", "get_pc_video_url"));
list.add(new BasicNameValuePair("section_id", "12179"));
list.add(new BasicNameValuePair("user_id", "5211314"));
list.add(new BasicNameValuePair("agency_id", "7"));
list.add(new BasicNameValuePair("hash", "b8b42c50b5ce8b68753a1ef3db688bde"));
//url格式编码
UrlEncodedFormEntity uefEntity = new UrlEncodedFormEntity(list,"UTF-8");
post.setEntity(uefEntity);
HttpEntity entity2 = post.getEntity();
//执行请求
CloseableHttpResponse httpResponse = httpClient.execute(post);
try{
HttpEntity entity = httpResponse.getEntity();
if (null != entity){
/* System.out.println(EntityUtils.toString(entity));*/
String retes = EntityUtils.toString(entity);
JSONObject jsonObject = new JSONObject(retes);
JSONObject personObject = jsonObject.getJSONObject("data");
System.out.println(personObject.getString("src"));
/* JSONObject json = JSONObject.fromObject(retes);
System.out.println(json);*/
/* String username = json.getString("src");
System.out.println("username==>" + username);*/
}
} finally{
httpResponse.close();
}
} catch( UnsupportedEncodingException e){
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
finally{
try{
closeHttpClient(httpClient);
} catch(Exception e){
e.printStackTrace();
}
}
}
private CloseableHttpClient getHttpClient(){
return HttpClients.createDefault();
}
private void closeHttpClient(CloseableHttpClient client) throws IOException{
if (client != null){
client.close();
}
}
}

浙公网安备 33010602011771号