您的位置:首页 > 理论基础 > 计算机网络

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;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息