使用HTTP GET 和HTTP POST请求访问HTTP资源
来源:互联网 发布:ronald jenkees知乎 编辑:程序博客网 时间:2024/06/07 15:49
Android SDK中,访问网络可以通过多种方式,如:HttpURLConnection, HttpGet, HttpPost
Android 集成的Apache HttpClient 模块是HttpClient4.0(org.apache.http.*),不是Jakarta Commons HttpClient 3.x(org.apache.commons.httpclient.*);
要通过HttpGet,HttpPost访问Http资源,必须通过以下3步:
1. 创建 HttpGet 或 HttpPost 对象,将要请求的URL通过构造方法传入HttpGet或HttpPost对象
2. 使用 DefaultHttpClient 类的execute方法发送Http Get或Post请求,并返回HttpResponse对象
3. 通过HttpResponse 接口的getEntity 方法返回响应信息,并进行相应的处理,如果使用HttpPost请求,还需要使用HttpPost 类的setEntity 方法设置请求参数
主要代码如下:
public class Main extends Activity implements OnClickListener {
@Override
public void onClick(View view) {
String url = "http://192.168.1.123/my_querybooks/QueryServlet";
TextView tvQueryResult = (TextView) findViewById(R.id.tvQueryResult);
EditText etBookName = (EditText) findViewById(R.id.etBookName);
HttpResponse httpResponse = null;
try {
switch (view.getId()) {
//提交Http Get 请求
case R.id.btnGetQuery:
//添加请求参数
url += "?bookname=" + etBookName.getText().toString();
// 第1步 创建HttpGet对象
HttpGet httpGet = new HttpGet(url);
// 第2步 使用execute 方法发送HTTP GET 请求,并返回HttpResponse 对象
httpResponse = new DefaultHttpClient().execute(httpGet);
// 判断响应状态码,如果为200表示服务端成功响应了客户端的请求
if (httpResponse.getStatusLine().getStatusCode() == 200)
{
// 第3步 使用getEntity 方法获得返回结果
String result = EntityUtils.toString(httpResponse.getEntity());
// 去掉返回结果中的 "\r" 字符,否则会在结果字符串后面显示一个小方格
tvQueryResult.setText(result.replaceAll("\r", ""));
}
break;
//提交Http Post 请求
case R.id.btnPostQuery:
// 第1步 创建HttpPost对象
HttpPost httpPost = new HttpPost(url);
// 设置Http Post请求参数必须用NameValuePair对象
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("bookname", etBookName.getText().toString()));
// 设置Post请求参数
httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
// 第2步 使用execute 方法发送HTTP Post 请求,并返回HttpResponse 对象
httpResponse = new DefaultHttpClient().execute(httpPost);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
// 第3步 使用getEntity 方法获得返回结果
String result = EntityUtils.toString(httpResponse.getEntity());
tvQueryResult.setText(result.replaceAll("\r", ""));
}
break;
}
} catch (Exception e) {
tvQueryResult.setText(e.getMessage());
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnGetQuery = (Button) findViewById(R.id.btnGetQuery);
Button btnPostQuery = (Button) findViewById(R.id.btnPostQuery);
btnGetQuery.setOnClickListener(this);
btnPostQuery.setOnClickListener(this);
}
}
示意图
具体代码请参见 ch09_httpgetpost 、my_querybooks 工程 (服务器端),必须先运行my_querybooks程序
- 使用HTTP GET 和HTTP POST请求访问HTTP资源
- 使用HTTP GET 和HTTP POST请求访问HTTP资源
- HTTP get和post请求
- http post和get请求
- http post get 请求
- HTTP GET/POST 请求
- http post get请求
- http请求 get post
- http post get请求
- HTTP请求GET,POST
- Http请求 post get
- Http协议(get请求和post请求)
- HTTP post请求和get请求
- HTTP的GET请求和POST请求
- HTTP请求的GET和POST请求
- HTTP的GET请求和POST请求
- WebView的使用(http协议访问网络Get和Post请求方式)
- HTTP-GET和HTTP-POST- -
- 【DP求最大子矩阵面积】hdu 1506
- 22323
- jQuery插件开发全解析
- linux cmd
- JAVA面试题大全
- 使用HTTP GET 和HTTP POST请求访问HTTP资源
- 错误: _malloc 已经在 Libcmtd.lib 中定义, 引发的思考
- 【最大子矩阵】hdu 2870
- 插入排序
- 合并排序
- 冒泡排序
- 堆排序
- 《COM 原理与应用》学习笔记 - 第一部分 COM原理收藏
- 利用堆之优先队列