Android-网络提交数据,接收数据

普通方式
GET
String param1 = URLEncoder.encode(name);
String param2 = URLEncoder.encode(password);
URL url = new URL(path + "?name=" + param1 + "&password=" + param2);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setReadTimeout(5000);
// 数据并没有发送给服务器
// 获取服务器返回的流信息
InputStream is = conn.getInputStream();
byte[] result = StreamTool.getBytes(is);
return new String(result);

-------------------------------------------------------------------------------
POST
String param1 = URLEncoder.encode(name);
String param2 = URLEncoder.encode(password);
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
String data = "name=" + param1 + "&password=" + param2;
conn.setRequestMethod("POST");
conn.setConnectTimeout(5000);
// 设置 http协议可以向服务器写数据
conn.setDoOutput(true);
// 设置http协议的消息头
conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", data.length() + "");
// 把我们准备好的data数据写给服务器
OutputStream os = conn.getOutputStream();
os.write(data.getBytes());
// httpurlconnection 底层实现 outputstream 是一个缓冲输出流
// 只要我们获取任何一个服务器返回的信息 , 数据就会被提交给服务器 , 得到服务器返回的流信息
int code = conn.getResponseCode();
if (code == 200) {
InputStream is = conn.getInputStream();
byte[] result = StreamTool.getBytes(is);
return new String(result);
} else {
throw new IllegalStateException("服务器状态异常");
}

----------------------------------------------------------------------------
client方式提交数据
GET方式

String message = null;
HttpClient client = new DefaultHttpClient();
if ("".equals(username) || "".equals(password)) {
return new String("用户名密码不能为空");
}
username = URLEncoder.encode(username);
password = URLEncoder.encode(password);
HttpGet httpget = new HttpGet(path + "?username=" + username+ "&password=" + password);
HttpResponse response = client.execute(httpget);
int code = response.getStatusLine().getStatusCode();
if (code == 200) {
InputStream in = response.getEntity().getContent();// 获得响应体,获得响应主体内容
byte[] dataByte = DataUtils.Stream2ByteArray(in);
message = new String(dataByte);
} else {
throw new IllegalStateException("服务器状态异常");
}
return message;

----------------------------------------------------------------------------
POST方式

String message = null;
HttpClient client = new DefaultHttpClient();
if ("".equals(username) || "".equals(password)) {
return new String("用户名密码不能为空");
}
username = URLEncoder.encode(username);
password = URLEncoder.encode(password);
HttpPost httppost = new HttpPost(path);
List<NameValuePair> parameters = new ArrayList<NameValuePair>();
parameters.add(new BasicNameValuePair("username", username));
parameters.add(new BasicNameValuePair("password", password));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(parameters,"UTF-8");
httppost.setEntity(entity);
HttpResponse response = client.execute(httppost);
int code = response.getStatusLine().getStatusCode();
if (code == 200) {
InputStream in = response.getEntity().getContent();
byte[] byteArray = DataUtils.Stream2ByteArray(in);
message = new String(byteArray);
}else{
throw new IllegalStateException("服务器异常");
}
return message;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值