第一种 不用传参数:
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请求方式