转载自:http://blog.csdn.net/zuolongsnail/article/details/6373051

Android应用经常会和服务器端交互,这就需要手机客户端发送网络请求,下面介绍四种常用网络请求方式,我这边是通过Android单元测试来完成这四种方法的,还不清楚Android的单元测试的同学们请看Android开发技巧总结中的Android单元测试的步骤一文。

java.net包中的HttpURLConnection类

Get方式:

// Get方式请求public static void requestByGet() throws Exception {String path = "https://reg.163.com/logins.jsp?id=helloworld&pwd=android";// 新建一个URL对象URL url = new URL(path);// 打开一个HttpURLConnection连接HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();// 设置连接超时时间urlConn.setConnectTimeout(5 * 1000);// 开始连接urlConn.connect();// 判断请求是否成功if (urlConn.getResponseCode() == HTTP_200) {// 获取返回的数据byte[] data = readStream(urlConn.getInputStream());Log.i(TAG_GET, "Get方式请求成功,返回数据如下:");Log.i(TAG_GET, new String(data, "UTF-8"));} else {Log.i(TAG_GET, "Get方式请求失败");}// 关闭连接urlConn.disconnect();}
Post方式:
// Post方式请求public static void requestByPost() throws Throwable {String path = "https://reg.163.com/logins.jsp";// 请求的参数转换为byte数组String params = "id=" + URLEncoder.encode("helloworld", "UTF-8")+ "&pwd=" + URLEncoder.encode("android", "UTF-8");byte[] postData = params.getBytes();// 新建一个URL对象URL url = new URL(path);// 打开一个HttpURLConnection连接HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();// 设置连接超时时间urlConn.setConnectTimeout(5 * 1000);// Post请求必须设置允许输出urlConn.setDoOutput(true);// Post请求不能使用缓存urlConn.setUseCaches(false);// 设置为Post请求urlConn.setRequestMethod("POST");urlConn.setInstanceFollowRedirects(true);// 配置请求Content-TypeurlConn.setRequestProperty("Content-Type","application/x-www-form-urlencode");// 开始连接urlConn.connect();// 发送请求参数DataOutputStream dos = new DataOutputStream(urlConn.getOutputStream());dos.write(postData);dos.flush();dos.close();// 判断请求是否成功if (urlConn.getResponseCode() == HTTP_200) {// 获取返回的数据byte[] data = readStream(urlConn.getInputStream());Log.i(TAG_POST, "Post请求方式成功,返回数据如下:");Log.i(TAG_POST, new String(data, "UTF-8"));} else {Log.i(TAG_POST, "Post方式请求失败");}}

org.apache.http包中的HttpGet和HttpPost类

Get方式:

// HttpGet方式请求public static void requestByHttpGet() throws Exception {String path = "https://reg.163.com/logins.jsp?id=helloworld&pwd=android";// 新建HttpGet对象HttpGet httpGet = new HttpGet(path);// 获取HttpClient对象HttpClient httpClient = new DefaultHttpClient();// 获取HttpResponse实例HttpResponse httpResp = httpClient.execute(httpGet);// 判断是够请求成功if (httpResp.getStatusLine().getStatusCode() == HTTP_200) {// 获取返回的数据String result = EntityUtils.toString(httpResp.getEntity(), "UTF-8");Log.i(TAG_HTTPGET, "HttpGet方式请求成功,返回数据如下:");Log.i(TAG_HTTPGET, result);} else {Log.i(TAG_HTTPGET, "HttpGet方式请求失败");}}

Post方式:
// HttpPost方式请求public static void requestByHttpPost() throws Exception {String path = "https://reg.163.com/logins.jsp";// 新建HttpPost对象HttpPost httpPost = new HttpPost(path);// Post参数List<NameValuePair> params = new ArrayList<NameValuePair>();params.add(new BasicNameValuePair("id", "helloworld"));params.add(new BasicNameValuePair("pwd", "android"));// 设置字符集HttpEntity entity = new UrlEncodedFormEntity(params, HTTP.UTF_8);// 设置参数实体httpPost.setEntity(entity);// 获取HttpClient对象HttpClient httpClient = new DefaultHttpClient();// 获取HttpResponse实例HttpResponse httpResp = httpClient.execute(httpPost);// 判断是够请求成功if (httpResp.getStatusLine().getStatusCode() == HTTP_200) {// 获取返回的数据String result = EntityUtils.toString(httpResp.getEntity(), "UTF-8");Log.i(TAG_HTTPGET, "HttpPost方式请求成功,返回数据如下:");Log.i(TAG_HTTPGET, result);} else {Log.i(TAG_HTTPGET, "HttpPost方式请求失败");}}



更多相关文章

  1. 【阿里云镜像】切换阿里巴巴开源镜像站镜像——Debian镜像
  2. Android(安卓)横竖屏切换时候重新进行数据请求问题
  3. android 流量管理
  4. Android(安卓)Wi-Fi基本操作
  5. Android:自定义控件你应该知道的这些事_TypedArray
  6. android中九宫格布局的实现
  7. Android(安卓)文件打开方式
  8. Android(安卓)不通过parent实现样式继承
  9. 聊聊 Android(安卓)的网络请求框架 Retrofit 2 + okhttp 3

随机推荐

  1. android基础(2)
  2. 36 Android(安卓)Activity跳转动画 获取
  3. SeeKBarTest
  4. 使用照相机开启闪光灯
  5. Android调用邮件客户端
  6. Android(安卓)监控网络状态
  7. android项目迁移到androidX:类映射(android
  8. ListView-arrayAdapter
  9. ANDROID媒体文件扫描
  10. OKhttp上传头像,调用相机相册进行裁剪