//httpget方法
public static String httpGet(String url) {
String result = "";
HttpClient httpClient = new DefaultHttpClient();
HttpGet get = new HttpGet(url);
try {
HttpResponse response = httpClient.execute(get);
if(response.getStatusLine().getStatusCode()==200){
//result是服务器得到的返回值
result = EntityUtils.toString(response.getEntity());
return result;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
//httppost 向服务器传递数据的方法
<pre name="code" class="java"><pre name="code" class="java">public static String doPost(String url,String execResult){
String result = "";
// 向服务器传送数据
HttpPost httpPost = new HttpPost(url);//创建一个list 来接收需要上传的数据
List<NameValuePair> params = new ArrayList<NameValuePair>();
// "xxx" 为服务器那边的key值 "mine"是你需要上传至服务器的值。
params.add(new BasicNameValuePair("xxx","mine"));
params.add(new BasicNameValuePair("xxx", "mine"));
// 绑定到entity
try { StringEntity entity = new StringEntity(params.toString());
httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
HttpResponse response = new DefaultHttpClient().execute(httpPost);
if (response.getStatusLine().getStatusCode() == 200)
{result = EntityUtils.toString(response.getEntity());}}
catch (Exception e)
{result = "提交失败";e.printStackTrace();}return result;}