Android 網(wǎng)絡(luò)編程中最經(jīng)常遇到的就是Http請(qǐng)求嘉竟,和處理返回值得到信息,達(dá)到網(wǎng)絡(luò)交互的效果翘盖;
下面看幾種最常用的網(wǎng)絡(luò)請(qǐng)求:
一 Http GET請(qǐng)求 (200響應(yīng))
private void HttpGet() {
try {
URL url = new URL("http://hbydhw.bestv.com.cn:8082/" +
"EDS/jsp/AuthenticationURL?Action=Login&UserID=test2018030701");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
String readLine = "";
if(connection.getResponseCode() == HttpURLConnection.HTTP_OK){
InputStream is = connection.getInputStream();
byte[] buffer = new byte[1024];
int hasread = 0;
while((hasread = is.read(buffer))>0){
readLine += new String(buffer,0,hasread);
}
JSONObject jsonObject = JSONObject.fromObject(readLine);
String name = jsonObject.getString("name");
int number = jsonObject.getInt("number");
}else{
//其他響應(yīng)
}
}catch (MalformedURLException e){
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
}
二 Http GET請(qǐng)求 (302響應(yīng))
/**
* 302響應(yīng)
* 獲取到響應(yīng)頭返回的
* Location地址
* 主機(jī)地址等信息
*/
private void getHttp302() {
try {
URL myUrl = new URL("");
HttpURLConnection connection = (HttpURLConnection) myUrl.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
// 必須設(shè)置false澎嚣,否則會(huì)自動(dòng)redirect到Location的地址
connection.setInstanceFollowRedirects(false);
if(connection.getResponseCode() == HttpURLConnection.HTTP_MOVED_TEMP){
String location = connection.getHeaderField("Location");
String temp = location.substring(location.indexOf("http://") + 2, location.length());
String returnip = temp.substring(0, temp.indexOf("/"));
returnip = "http://" + returnip;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
三 Http POST請(qǐng)求
/**
* http POST
* 響應(yīng)
*/
private void getHttpPost() {
try {
String uri = "http://hbydfh.bestv.com.cn:6060/aaa/services/rest/V2/AAA/xxxxxx";
URL url = new URL(uri);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
connection.setRequestProperty("Accept", "application/json");
//POST請(qǐng)求中寫入?yún)?shù)
OutputStream out1 = connection.getOutputStream();
JSONObject jsonMacInput = new JSONObject();
jsonMacInput.put("key", "value");
out1.write((jsonMacInput.toString()).getBytes());
out1.flush();
out1.close();
if(connection.getResponseCode() == HttpURLConnection.HTTP_OK){
InputStream is1 = connection.getInputStream();
byte[] buffer1 = new byte[1024];
int hasread = 0;
while ((hasread = is1.read(buffer1)) > 0) {
returnaccount += new String(buffer1, 0, hasread);
}
//解析返回值
JSONObject jsonOutPut = JSONObject.fromObject(returnaccount);
resultCode = jsonOutPut.getString("returnKey");
is1.close();
connection.disconnect();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}