Android-网络提交数据,接收数据
2012-07-08 12:17
253 查看
普通方式
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;
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;
相关文章推荐
- android 学习随笔十(网络:get、post提交数据)
- Android :get传值 向网络提交数据
- android网络通信 接收和发送数据详解
- android网络编程之post提交数据
- 25.android网络编程-get方式提交数据(HTML源文件查看器)
- android网络通信 接收和发送数据详解
- Android 网络提交数据(使用Asynchronous Http Client)
- Android网络数据之向服务器提交数据的三种方式(get+post+AsyncHttpClient)
- Android-网络提交数据get和post的两种方式
- android网络数据提交类HttpUtils
- Android学习文档之数据提交到网络
- Android 网络提交数据(使用Asynchronous Http Client)
- Android网络编程之使用get方式向服务端提交数据和乱码问题的解决
- android重新学_网络3——采用HttpURLConnection提交数据到服务器
- android网络通信 接收和发送数据详解
- Androidx学习笔记(36)---网络提交数据的方式--GET方式提交数据
- Android网络编程之使用post方式提交数据
- Androidx学习笔记(37)--- 网络提交数据的方式--POST方式提交数据
- android中通过GET方法接收数据并通过xml解析数据,POST方法向服务器提交数据
- Android原生模拟表单提交上传多图+PHP接收表单数据和多图存储