Android HTTP操作(三)
来源:互联网 发布:淘宝卖家学院光云科技 编辑:程序博客网 时间:2024/05/01 06:06
1.HTTP请求的方法
2.使用Get方法发送请求
private HttpResponse httpResponse = null;
private HttpEntity httpEntity = null;
private String baseUrl = "Http://192.168.0.1:8080/serverside/name";
public void onClick(View v) {
// TODO Auto-generated method stub
String name = nameView.getText().toString();
String age = ageView.getText().toString();
String url = baseUrl + "?" + "name=" + name + "age=" + age;
//生成一个请求对象
HttpGet httpGet = new HttpGet("url");
//生成一个HTTP客户端对象
HttpClient httpClient = new DefaultHttpClient();
//使用HTTP客户端发送请求对象
InputStream inputStream = null;
try {
httpResponse = httpClient.execute(httpGet); //调用客户端对象的execute方法把请求对象传进去,就相当于向服务端发送了一次请求,然后服务器端会给一个相遇HttpResponse
httpEntity = httpResponse.getEntity(); // 得到一个服务器端响应的内容
inputStream = httpEntity.getContent(); // 把内容放到一个流里面
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String result = "";
String line = "";
while ((line = reader.readLine()) != null){
result = result + line;
}
System.out.println(result);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try
{
inputStream.close();
}catch(Exception e){
e.printStackTrace();
}
}
3.使用Post放发发送请求
private String baseUrl = "Http://192.168.0.1:8080/serverside/name";
private HttpResponse httpResponse = null;
private HttpEntity httpEntity = null;
public void onClick(View v) {
// TODO Auto-generated method stub
String name = nameView.getText().toString();
String age = ageView.getText().toString();
NameValuePair nameValuePair1 = new BasicNameValuePair("name", name);
NameValuePair nameValuePair2 = new BasicNameValuePair("age", age);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(nameValuePair1);
nameValuePairs.add(nameValuePair2);
try{
HttpEntity requestEntity = new UrlEncodedFormEntity(nameValuePairs);
HttpPost httpPost = new HttpPost(baseUrl);
HttpPost.setEntity(requestEntity);
HttpClient httpClient = new DefaultHttpClient();
InputStream inputStream = null;
try {
httpResponse = httpClient.execute(httpPost); //调用客户端对象的execute方法把请求对象传进去,就相当于向服务端发送了一次请求,然后服务器端会给一个相遇HttpResponse
httpEntity = httpResponse.getEntity(); // 得到一个服务器端响应的内容
inputStream = httpEntity.getContent(); // 把内容放到一个流里面
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String result = "";
String line = "";
while ((line = reader.readLine()) != null){
result = result + line;
}
System.out.println(result);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try
{
inputStream.close();
}catch(Exception e){
e.printStackTrace();
}
}
}catch(Exception e) {
e.printStackTrace();
}
};
Get和Post方法的区别
在于传递数据方式的区别
Get方法
String name = nameView.getText().toString();
String age = ageView.getText().toString();
String url = baseUrl + "?" + "name=" + name + "age=" + age;
HttpGet httpGet = new HttpGet("url");
//生成一个HTTP客户端对象
HttpClient httpClient = new DefaultHttpClient();
//使用HTTP客户端发送请求对象
InputStream inputStream = null;
Post方法
String name = nameView.getText().toString();
String age = ageView.getText().toString();
NameValuePair nameValuePair1 = new BasicNameValuePair("name", name);
NameValuePair nameValuePair2 = new BasicNameValuePair("age", age);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(nameValuePair1);
nameValuePairs.add(nameValuePair2);
try{
HttpEntity requestEntity = new UrlEncodedFormEntity(nameValuePairs);
HttpPost httpPost = new HttpPost(baseUrl);
HttpPost.setEntity(requestEntity);
HttpClient httpClient = new DefaultHttpClient();
InputStream inputStream = null;
- Android HTTP操作(三)
- Android HTTP操作(二)
- Android之Http网络编程(三)
- Android开发--Http操作介绍(一)
- Android开发--Http操作介绍(二)
- 浅析 Android操作HTTP
- Android HTTP操作
- android学习--操作http
- Android http操作
- Android HTTP操作
- android的文件、目录操作(三)
- Android蓝牙开发(三):操作步骤
- Android 数据操作(三)Json解析
- Android手势操作(三)
- android开发中的http操作
- Android入门--Http网络操作
- 【Android技术整理】HTTP操作
- Android 操作HTTP實現與服務器通信
- 子集生成
- 虚拟机找不到/mnt/hgfs挂载目录
- RecyclerView自定义ItemDecoration
- mysql启动失败:mysql服务无法启动 服务没有报告任何错误 排查过程及解决方法
- Dumb-Init进程信号处理
- Android HTTP操作(三)
- iOS 开发command+左键 不能索引 出现问号 Xcode一直处于indexing状态
- 一些有用的SAP技术TCODE
- 交换两个数据的方法
- pcntl_fock & pcntl_waitpid 初学
- iOS知识点八
- Java Web Project中文乱码
- Zookeeper WINDOWS 安装配置
- C++中的深拷贝与浅拷贝