android中使用get、post和HttpClient方式与服务器交互
来源:互联网 发布:oracle数据库pdf下载 编辑:程序博客网 时间:2024/06/08 16:04
一:get方式请求服务器
public String byGet(){
try {
URL url=new URL(http://www.apkbus.com/index.jsp?id=6&username=blog);
HttpURLConnection conn= (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setReadTimeout(5000);
conn.setConnectTimeout(5000);
conn.setRequestProperty(field, newValue);
int code=conn.getResponseCode();
if (code==200) {
//请求成功
InputStream in=conn.getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(in, "UTF-8"));
StringBuffer buffer=null;
while(br.readLine()!=null){
buffer.append(br.readLine().trim());
}
return buffer.toString();
} else {
return "请求失败";
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
URL url=new URL(http://www.apkbus.com/index.jsp?id=6&username=blog);
HttpURLConnection conn= (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setReadTimeout(5000);
conn.setConnectTimeout(5000);
conn.setRequestProperty(field, newValue);
int code=conn.getResponseCode();
if (code==200) {
//请求成功
InputStream in=conn.getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(in, "UTF-8"));
StringBuffer buffer=null;
while(br.readLine()!=null){
buffer.append(br.readLine().trim());
}
return buffer.toString();
} else {
return "请求失败";
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
二:post方式提交数据
public String byPost(){
try {
URL url=new URL("http://www.apkbus.com/");
HttpURLConnection conn= (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setReadTimeout(5000);
conn.setConnectTimeout(5000);
String data="username=4&id=4";
conn.setRequestProperty("Content-Type", "");//指定请求类型
conn.setRequestProperty("Content-Length", data.length()+"");//指定数据的大小
conn.setDoOutput(true);//指定允许往服务器些数据
OutputStream os=conn.getOutputStream();
os.write(data.getBytes());//写数据
int code=conn.getResponseCode();
if (code==200) {
//请求成功
InputStream in=conn.getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(in, "UTF-8"));
StringBuffer buffer=null;
while(br.readLine()!=null){
buffer.append(br.readLine().trim());
}
return buffer.toString();
} else {
return "请求失败";
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
try {
URL url=new URL("http://www.apkbus.com/");
HttpURLConnection conn= (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setReadTimeout(5000);
conn.setConnectTimeout(5000);
String data="username=4&id=4";
conn.setRequestProperty("Content-Type", "");//指定请求类型
conn.setRequestProperty("Content-Length", data.length()+"");//指定数据的大小
conn.setDoOutput(true);//指定允许往服务器些数据
OutputStream os=conn.getOutputStream();
os.write(data.getBytes());//写数据
int code=conn.getResponseCode();
if (code==200) {
//请求成功
InputStream in=conn.getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(in, "UTF-8"));
StringBuffer buffer=null;
while(br.readLine()!=null){
buffer.append(br.readLine().trim());
}
return buffer.toString();
} else {
return "请求失败";
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
三:使用HttpClient开源框架的GET方法
public String byHttpClientGET(){
try {
//1.打开一个浏览器
HttpClient client=new DefaultHttpClient();
//2.输入地址
//2.get方式输入地址
HttpGet httpget=new HttpGet("http://www.apkbus.com/index.jsp?id=3");
//3.回车
HttpResponse response=client.execute(httpget);
int code=response.getStatusLine().getStatusCode();
if (code==200) {
//请求成功
HttpEntity entity=response.getEntity();
InputStream in= entity.getContent();
BufferedReader br=new BufferedReader(new InputStreamReader(in, "UTF-8"));
StringBuffer buffer=null;
while(br.readLine()!=null){
buffer.append(br.readLine().trim());
}
return buffer.toString();
} else {
return "请求失败";
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
try {
//1.打开一个浏览器
HttpClient client=new DefaultHttpClient();
//2.输入地址
//2.get方式输入地址
HttpGet httpget=new HttpGet("http://www.apkbus.com/index.jsp?id=3");
//3.回车
HttpResponse response=client.execute(httpget);
int code=response.getStatusLine().getStatusCode();
if (code==200) {
//请求成功
HttpEntity entity=response.getEntity();
InputStream in= entity.getContent();
BufferedReader br=new BufferedReader(new InputStreamReader(in, "UTF-8"));
StringBuffer buffer=null;
while(br.readLine()!=null){
buffer.append(br.readLine().trim());
}
return buffer.toString();
} else {
return "请求失败";
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
四:使用HttpClient开源框架的POST方法
public String byHttpClientPOST(){
try {
//1.打开一个浏览器
HttpClient client=new DefaultHttpClient();
//2.输入地址
//2.get方式输入地址
HttpPost httpPOST=new HttpPost("http://www.apkbus.com");
//3.指定要提交的数据实体
ArrayList parameters=new ArrayList<NameValuePair>();
parameters.add(new BasicNameValuePair("username", "zhangsan"));
httpPOST.setEntity(new UrlEncodedFormEntity(parameters, "UTF-8"));
//4.敲回车
HttpResponse response=client.execute(httpPOST);
int code=response.getStatusLine().getStatusCode();
if (code==200) {
//请求成功
HttpEntity entity=response.getEntity();
InputStream in= entity.getContent();
BufferedReader br=new BufferedReader(new InputStreamReader(in, "UTF-8"));
StringBuffer buffer=null;
while(br.readLine()!=null){
buffer.append(br.readLine().trim());
}
return buffer.toString();
} else {
return "请求失败";
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
try {
//1.打开一个浏览器
HttpClient client=new DefaultHttpClient();
//2.输入地址
//2.get方式输入地址
HttpPost httpPOST=new HttpPost("http://www.apkbus.com");
//3.指定要提交的数据实体
ArrayList parameters=new ArrayList<NameValuePair>();
parameters.add(new BasicNameValuePair("username", "zhangsan"));
httpPOST.setEntity(new UrlEncodedFormEntity(parameters, "UTF-8"));
//4.敲回车
HttpResponse response=client.execute(httpPOST);
int code=response.getStatusLine().getStatusCode();
if (code==200) {
//请求成功
HttpEntity entity=response.getEntity();
InputStream in= entity.getContent();
BufferedReader br=new BufferedReader(new InputStreamReader(in, "UTF-8"));
StringBuffer buffer=null;
while(br.readLine()!=null){
buffer.append(br.readLine().trim());
}
return buffer.toString();
} else {
return "请求失败";
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
- android中使用get、post和HttpClient方式与服务器交互
- Android使用HttpClient以Post、Get请求服务器发送数据的方式(普通和json)
- Android使用HttpClient以Post、Get请求服务器发送数据的方式(普通和json)
- Android使用HttpClient以Post、Get请求服务器发送数据的方式(普通和json)
- Android中使用HttpClient 服务 GET,POST和上传
- J2ME HTTP方式与服务器交互信息:GET方式和POST方式
- Android通过Get,Post,HttpClient方式提交参数给服务器
- Android通过Get,Post,HttpClient方式提交参数给服务器
- Android 使用 HttpClient 进行网络通信,包括Get方式和Post方式
- Android 使用 HttpClient 进行网络通信,包括Get方式和Post方式
- Android 使用 HttpClient 进行网络通信,包括Get方式和Post方式
- Android 使用 HttpClient 进行网络通信,包括Get方式和Post方式(转)
- 浏览器和服务器交互方式GET和POST的区别
- Android使用HttpClient完成Post和Get方式的联网请求
- 关于 Android 中使用get和post方式的区别与优缺点
- Android 使用GET和PUT方式以及用HttpClient方式向web服务器提交参数
- HttpClient get和HttpClient Post请求的方式获取服务器的返回数据
- HttpClient get和HttpClient Post请求的方式获取服务器的返回数据
- adb server is out of date killing
- oracle cmd sqlplus 数据导入 数据导出
- 为什么你一直没有成就?
- 在Xcode中进行自动化测试
- SQL 2008中怎么打开表并修改
- android中使用get、post和HttpClient方式与服务器交互
- JAVA线程揭秘
- 在处理链中调用APD
- php反射类 ReflectionClass
- java 从零开始,学习笔记之基础入门<封装类>(十一)
- SQL Syntax illustration
- struts2的国际化
- wince下speex消回音
- 程序员常用的五个文本编辑器