第一種 不用傳參數:
public String getAdminPwd() {
String path = "http://10.10.10.20:85/WebSrv/GetPwd";
//先通過位址擷取流
InputStream is = NetUtils.getInputStreamByURI(path);
String pwd = null;
try {
//在通過流來擷取json字元串
String info = NetUtils.getStringByInputStream(is);
//解析json
JSONObject jo = new JSONObject(info);
if (jo != null) {
String retcode = jo.getString("retcode");
if (retcode.equals("0")) {
String retmsg = jo.getString("retmsg");
pwd = retmsg;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return pwd;
}
// 通過Uri擷取流
public static InputStream getInputStreamByURI(String path) {
// 建立HttpClient對象
HttpClient client = new DefaultHttpClient();
// 建立HttpPost請求方法
HttpPost request = new HttpPost();
// 設定URI位址
try {
request.setURI(new URI(path));
} catch (URISyntaxException e) {
e.printStackTrace();
}
HttpResponse response = null;
try {
response = client.execute(request);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
InputStream is = null;
try {
is = response.getEntity().getContent();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return is;
}
// 通過流擷取字元串
public static String getStringByInputStream(InputStream is)
throws UnsupportedEncodingException {
StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(is,
"UTF-8"));
String temp = null;
try {
while ((temp = br.readLine()) != null) {
sb.append(temp);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
第二種 需要傳參:
// 登陸/退出上傳日志
public boolean upLoadLog(String str) {
String path = "http://10.10.10.20:85/WebSrv/WriteLog";
// InputStream is = NetUtils.getInputStreamByURI(path);
// String info = NetUtils.getStringByInputStream(is);
boolean isOk = false;
//設定參數 可設定多個
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("data", str));
try {
//得到json字元串
String info = NetUtils.doPost(params, path);
JSONObject jo = new JSONObject(info);
if (jo != null) {
String retcode = jo.getString("retcode");
if (retcode.equals("0")) {
isOk = true;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return isOk;
}
// 通過Uri擷取json字元串
public static String doPost(List<NameValuePair> params, String url)
throws Exception {
String result = null;
// 擷取HttpClient對象
HttpClient httpClient = new DefaultHttpClient();
// 建立HttpPost對象
HttpPost httpPost = new HttpPost(url);
if (params != null) {
// 設定字元集
HttpEntity entity = new UrlEncodedFormEntity(params, HTTP.UTF_8);
// 設定參數實體
httpPost.setEntity(entity);
}
/*
* // 連接配接逾時 httpClient.getParams().setParameter(
* CoreConnectionPNames.CONNECTION_TIMEOUT, 3000); // 請求逾時
* httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,
* 3000);
*/
// 擷取HttpResponse執行個體
HttpResponse httpResp = httpClient.execute(httpPost);
// 擷取傳回的資料
result = EntityUtils.toString(httpResp.getEntity(), "UTF-8");
return result;
}
以上就是本人所用過的Http post請求方式