(1)GET方法

    /**
* 根據高德地圖api獲取位置資訊
* @return
*
*/
public static String getMapAddInfo(String httpurl) {
HttpURLConnection connection = null;
InputStream is = null;
BufferedReader br = null;
String result = null;// 返回結果字串
try {
// 建立遠端url連線物件
URL url = new URL("http://restapi.amap.com/v3/geocode/geo?address=陝西西安
&output=XML&key=b6acf6e6c98f32be96e757ede9a9aee3");
// 通過遠端url連線物件開啟一個連線,強轉成httpURLConnection類
connection = (HttpURLConnection) url.openConnection();
// 設定連線方式:get
connection.setRequestMethod("GET");
// 設定連線主機伺服器的超時時間:15000毫秒
connection.setConnectTimeout(15000);
// 設定讀取遠端返回的資料時間:60000毫秒
connection.setReadTimeout(60000);
// 傳送請求
connection.connect();
// 通過connection連線,獲取輸入流
if (connection.getResponseCode() == 200) {
is = connection.getInputStream();
// 封裝輸入流is,並指定字符集
br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
// 存放資料
StringBuffer sbf = new StringBuffer();
String temp = null;
while ((temp = br.readLine()) != null) {
sbf.append(temp);
sbf.append("\r\n");
}
result = sbf.toString();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 關閉資源
if (null != br) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != is) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
connection.disconnect();// 關閉遠端連線
}
System.out.println(result);
return result;
}

返回的xml格式化後:

<?xml version="1.0" encoding="UTF-8"?>
<response>
<status>1</status>
<info>OK</info>
<infocode>10000</infocode>
<count>1</count>
<geocodes type="list">
<geocode>
<formatted_address>陝西省西安市</formatted_address>
<country>中國</country>
<province>陝西省</province>
<citycode>029</citycode>
<city>西安市</city>
<district></district>
<township></township>
<neighborhood>
<name></name>
<type></type>
</neighborhood>
<building>
<name></name>
<type></type>
</building>
<adcode>610100</adcode>
<street></street>
<number></number>
<location>108.940174,34.341568</location>
<level>市</level>
</geocode>
</geocodes>
</response>

(2)POST方法

/**
* 根據介面獲取資訊
* @return
* @throws IOException
*/
public static String postMapAddInfo(String httpUrl, String param) {
HttpURLConnection connection = null;
InputStream is = null;
OutputStream os = null;
BufferedReader br = null;
String result = null;
try {
URL url = new URL("http://localhost:8080/getAll");
// 通過遠端url連線物件開啟連線
connection = (HttpURLConnection) url.openConnection();
// 設定連線請求方式
connection.setRequestMethod("POST");
// 設定連線主機伺服器超時時間:15000毫秒
connection.setConnectTimeout(15000);
// 設定讀取主機伺服器返回資料超時時間:60000毫秒
connection.setReadTimeout(60000);
// 預設值為:false,當向遠端伺服器傳送資料/寫資料時,需要設定為true
connection.setDoOutput(true);
// 預設值為:true,當前向遠端服務讀取資料時,設定為true,該引數可有可無
connection.setDoInput(true);
// 設定傳入引數的格式:請求引數應該是 name1=value1&name2=value2 的形式。
connection.setRequestProperty("Content-Type", "application/json");
// 通過連線物件獲取一個輸出流
os = connection.getOutputStream();
// 通過輸出流物件將引數寫出去/傳輸出去,它是通過位元組陣列寫出的
os.write(param.getBytes());
// 通過連線物件獲取一個輸入流,向遠端讀取
if (connection.getResponseCode() == 200) { is = connection.getInputStream();
// 對輸入流物件進行包裝:charset根據工作專案組的要求來設定
br = new BufferedReader(new InputStreamReader(is, "UTF-8")); StringBuffer sbf = new StringBuffer();
String temp = null;
// 迴圈遍歷一行一行讀取資料
while ((temp = br.readLine()) != null) {
sbf.append(temp);
sbf.append("\r\n");
}
result = sbf.toString();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 關閉資源
if (null != br) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != os) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != is) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// 斷開與遠端地址url的連線
connection.disconnect();
}
System.out.println(result);
return result;
}

返回的json有涉密資訊不做資料解析

(3)POST方法 程式碼實現方法傳form-data引數

注意:一定注意引數應是拼接傳入,例如:String str = "name = zxl&sex =男 "

/**
* 當使用此方法 connection.getResponseCode() 方法報401時 ,請設定請求頭,如何請求頭有問題則報401 無法訪問
* @param httpUrl
* @param param
* @return
*/
public static String doPost(String httpUrl, String param) { HttpURLConnection connection = null;
InputStream is = null;
OutputStream os = null;
BufferedReader br = null;
String result = null;
try {
URL url = new URL(httpUrl);
// 通過遠端url連線物件開啟連線
connection = (HttpURLConnection) url.openConnection();
// 設定連線請求方式
connection.setRequestMethod("POST");
// 設定連線主機伺服器超時時間:15000毫秒
connection.setConnectTimeout(15000);
// 設定讀取主機伺服器返回資料超時時間:60000毫秒
connection.setReadTimeout(60000); // 預設值為:false,當向遠端伺服器傳送資料/寫資料時,需要設定為true
connection.setDoOutput(true);
// 預設值為:true,當前向遠端服務讀取資料時,設定為true,該引數可有可無
connection.setDoInput(true);
// 設定傳入引數的格式:請求引數應該是 name1=value1&name2=value2 的形式。
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
// 通過連線物件獲取一個輸出流
os = connection.getOutputStream();
// 通過輸出流物件將引數寫出去/傳輸出去,它是通過位元組陣列寫出的
os.write(param.getBytes());
// 通過連線物件獲取一個輸入流,向遠端讀取
if (connection.getResponseCode() == 200) { is = connection.getInputStream();
// 對輸入流物件進行包裝:charset根據工作專案組的要求來設定
br = new BufferedReader(new InputStreamReader(is, "UTF-8")); StringBuffer sbf = new StringBuffer();
String temp = null;
// 迴圈遍歷一行一行讀取資料
while ((temp = br.readLine()) != null) {
sbf.append(temp);
sbf.append("\r\n");
}
result = sbf.toString();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 關閉資源
if (null != br) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != os) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != is) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// 斷開與遠端地址url的連線
connection.disconnect();
}
return result;
}