分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 前端开发

httpclient

发布时间:2023-09-06 02:05责任编辑:彭小芳关键词:http
 ???/* ????* 使用httpClient提交get请求 ????*/ ???public void testClientGet(View v) { ???????//1. 显示ProgressDialog ???????final ProgressDialog dialog = ProgressDialog.show(this, null, "正在请求中..."); ???????//2. 启动分线程 ???????new Thread(){ ???????????//3. 在分线程, 发送请求, 得到响应数据 ???????????public void run() { ???????????????try { ???????????????????//1). 得到path, 并带上参数name=Tom1&age=11 ???????????????????String path = et_network_url.getText().toString()+"?name=Tom3&age=13"; ???????????????????????????????????????//2). 创建HttpClient对象 ???????????????????HttpClient httpClient = new DefaultHttpClient(); ???????????????????//3). 设置超时 ???????????????????HttpParams params = httpClient.getParams(); ???????????????????HttpConnectionParams.setConnectionTimeout(params, 5000); ???????????????????HttpConnectionParams.setSoTimeout(params, 5000); ???????????????????//4). 创建请求对象 ???????????????????HttpGet request = new HttpGet(path); ???????????????????//5). 执行请求对象, 得到响应对象 ???????????????????HttpResponse response = httpClient.execute(request); ???????????????????????????????????????int statusCode = response.getStatusLine().getStatusCode(); ???????????????????if(statusCode==200) { ???????????????????????//6). 得到响应体文本 ???????????????????????HttpEntity entity = response.getEntity(); ???????????????????????final String result = EntityUtils.toString(entity); ???????????????????????//4. 要主线程, 显示数据, 移除dialog ???????????????????????runOnUiThread(new Runnable() { ???????????????????????????@Override ???????????????????????????public void run() { ???????????????????????????????et_network_result.setText(result); ???????????????????????????????dialog.dismiss(); ???????????????????????????} ???????????????????????}); ???????????????????} ???????????????????//7). 断开连接 ???????????????????httpClient.getConnectionManager().shutdown(); ???????????????} catch (Exception e) { ???????????????????e.printStackTrace(); ???????????????????//如果出了异常要移除dialog ???????????????????dialog.dismiss(); ???????????????} ???????????} ???????}.start(); ???} ???/* ????* 使用httpClient提交post请求 ????*/ ???public void testClientPost(View v) { ???????//1. 显示ProgressDialog ???????final ProgressDialog dialog = ProgressDialog.show(this, null, "正在请求中..."); ???????//2. 启动分线程 ???????new Thread(){ ???????????//3. 在分线程, 发送请求, 得到响应数据 ???????????public void run() { ???????????????try { ???????????????????//1). 得到path ???????????????????String path = et_network_url.getText().toString(); ???????????????????????????????????????//2). 创建HttpClient对象 ???????????????????HttpClient httpClient = new DefaultHttpClient(); ???????????????????//3). 设置超时 ???????????????????HttpParams params = httpClient.getParams(); ???????????????????HttpConnectionParams.setConnectionTimeout(params, 5000); ???????????????????HttpConnectionParams.setSoTimeout(params, 5000); ???????????????????//4). 创建请求对象 ???????????????????HttpPost request = new HttpPost(path); ???????????????????//设置请求体 ???????????????????List<BasicNameValuePair> parameters = new ArrayList<BasicNameValuePair>(); ???????????????????parameters.add(new BasicNameValuePair("name", "Tom4")); ???????????????????parameters.add(new BasicNameValuePair("age", "14")); ???????????????????HttpEntity entity = new UrlEncodedFormEntity(parameters); ???????????????????request.setEntity(entity); ???????????????????????????????????????//5). 执行请求对象, 得到响应对象 ???????????????????HttpResponse response = httpClient.execute(request); ???????????????????????????????????????int statusCode = response.getStatusLine().getStatusCode(); ???????????????????if(statusCode==200) { ???????????????????????//6). 得到响应体文本 ???????????????????????entity = response.getEntity(); ???????????????????????final String result = EntityUtils.toString(entity); ???????????????????????//4. 要主线程, 显示数据, 移除dialog ???????????????????????runOnUiThread(new Runnable() { ???????????????????????????@Override ???????????????????????????public void run() { ???????????????????????????????et_network_result.setText(result); ???????????????????????????????dialog.dismiss(); ???????????????????????????} ???????????????????????}); ???????????????????} ???????????????????//7). 断开连接 ???????????????????httpClient.getConnectionManager().shutdown(); ???????????????} catch (Exception e) { ???????????????????e.printStackTrace(); ???????????????????//如果出了异常要移除dialog ???????????????????dialog.dismiss(); ???????????????} ???????????} ???????}.start(); ???}

httpclient

原文地址:https://www.cnblogs.com/znsongshu/p/9345868.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved