HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(baseUrl + action);
HttpResponse response = httpclient.execute(httpget);
retStr = EntityUtils.toString(response.getEntity());
JSONObject objRoot = new JSONObject(retStr );
retStr = objRoot.getString("qwe");
所用到的jar 包:httpcore httpclient
http://www.cnblogs.com/Wen-yu-jing/p/3552154.html
public static String sendPost(String host,String content,String encodeType) throws UnsupportedEncodingException,IOException
{
HttpClient httpClient = new DefaultHttpClient();
HttpPost post= new HttpPost(host);
//NameValuePair p = new BasicNameValuePair("v", "123");
List<NameValuePair> list = new ArrayList<NameValuePair>();
list.add(new BasicNameValuePair("v", "1405322466697"));
StringEntity entity = new UrlEncodedFormEntity(list);
post.setEntity(entity);
StringEntity myEntity;
if(!encodeType.isEmpty())
myEntity = new StringEntity(content,encodeType);
else
myEntity = new StringEntity(content);
post.addHeader("Content-Type", "text/xml");
post.addHeader("Connection","keep-alive");
post.addHeader("Accept","*/*");
post.addHeader("Cache-Control", "max-age=0");
post.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)");
post.setEntity(myEntity);
HttpResponse response = httpClient.execute(post);
String answerRes = "";
if(response.getStatusLine().getStatusCode()==200)
{
//如果状态码为200,就是正常返回
String result=EntityUtils.toString(response.getEntity());
answerRes = new String(result.getBytes("iso-8859-1"), "UTF-8");
}
return answerRes;
}
public static String sendGet(String url) throws UnsupportedEncodingException,IOException
{
HttpClient httpClient = new DefaultHttpClient();
HttpGet get= new HttpGet(url);
get.addHeader("Connection","keep-alive");
get.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/;q=0.8");
get.addHeader("Cache-Control", "max-age=0");
get.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)");
HttpResponse response = httpClient.execute(get);
String answerRes = "";
if(response.getStatusLine().getStatusCode()==200)
{
//如果状态码为200,就是正常返回
String result=EntityUtils.toString(response.getEntity());
answerRes = new String(result.getBytes("iso-8859-1"), "UTF-8");
}
return answerRes;
}