//get方法
public void btGet(View view){
new Thread(){
@Override
public void run() {
getContent();
}
}.start();
}
//post方法
public void btPost(View view){
new Thread(){
@Override
public void run() { postContent("52cb54f14bc3bbcd9a96e1d13ee0cf2b");
}
}.start();
}
public void getContent(){
try {
//打开浏览器
HttpClient httpClient = new DefaultHttpClient();
//填写一个地址
HttpGet httpGet = new HttpGet("http://v.juhe.cn/expressonline/getCarriers.php?key=52cb54f14bc3bbcd9a96e1d13ee0cfb&ex_category="+ URLEncoder.encode("圆通","utf-8")+"");
//开始浏览并等待浏览器响应
HttpResponse response = httpClient.execute(httpGet);
//得到状态栏
int code = response.getStatusLine().getStatusCode();
if(code == 200){
InputStream is = response.getEntity().getContent();
String content = StreamTools.getStream(is);
System.out.println(content);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void postContent(String name){
try {
//打开浏览器
HttpClient httpClient = new DefaultHttpClient();
//给一个网址
HttpPost httpPost = new HttpPost("http://v.juhe.c/expressonline/getCarriers.php");
//请求的参数
List list = new ArrayList();
list.add(new BasicNameValuePair("key",name));
list.add(new BasicNameValuePair("ex_category","顺丰"));
httpPost.setEntity(new UrlEncodedFormEntity(list,"utf-8"));
//打开网址并等待浏览器响应
HttpResponse httpResponse = httpClient.execute(httpPost);
int code = httpResponse.getStatusLine().getStatusCode();
if(code == 200){
InputStream is = httpResponse.getEntity().getContent();
String s = StreamTools.getStream(is);
//第二种方式
//String string = EntityUtils.toString(httpResponse.getEntity(), "utf-8");
System.out.println(s);
}
} catch (IOException e) {
e.printStackTrace();
}
}