jsp android 交互,Android中HTTP几种请求和响应的代码实现

Android 网络编程中最经常遇到的就是Http请求,和处理返回值得到信息,达到网络交互的效果;

下面看几种最常用的网络请求:

一 Http GET请求 (200响应)

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{

//其他响应

}

}catch (MalformedURLException e){

e.printStackTrace();

} catch (IOException e){

e.printStackTrace();

}

}

二 Http GET请求 (302响应)

/**

* 302响应

* 获取到响应头返回的

* Location地址

* 主机地址等信息

*/

private void getHttp302() {

try {

URL myUrl = new URL("");

HttpURLConnection connection = (HttpURLConnection) myUrl.openConnection();

connection.setRequestMethod("GET");

connection.setRequestProperty("accept", "*/*");

connection.setRequestProperty("connection", "Keep-Alive");

// 必须设置false,否则会自动redirect到Location的地址

connection.setInstanceFollowRedirects(false);

if(connection.getResponseCode() == HttpURLConnection.HTTP_MOVED_TEMP){

String location = connection.getHeaderField("Location");

String temp = location.substring(location.indexOf("//") + 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请求

/**

* http POST

* 响应

*/

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请求中写入参数

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();

}

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值