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

java.net包中的HttpURLConnection类

Get方式:

1234567891011121314151617181920212223
// 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方式:

12345678910111213141516171819202122232425262728293031323334353637383940
// 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方式:

12345678910111213141516171819
// 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方式:

123456789101112131415161718192021222324252627
// 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. Android中使用xml保存数据(xml文件序列化)及使用pull解析xml文件
  2. android读取sqlite数据库的数据并用listview显示
  3. 【Android】数据存储之Shared Preferences
  4. 对android里布局文件当中的TextView对象设置事件监听,但是不响应
  5. android 截取验证码的两种实现方式
  6. 20172324 2017-2018《程序设计与数据结构》第十一周学习总结

随机推荐

  1. 【Android】App自动更新之通知栏下载
  2. [Android]_[ACtivity生命周期]
  3. android 更新界面视图 Handler和runOnUiT
  4. Android如何通过手机自动获取短信验证码
  5. 玩转Android---UI篇---EditText(编辑框)
  6. Android(安卓)Navigation Drawer简记
  7. Android(安卓)Audio AudioStreamOutALSA:
  8. Android中notifyDataSetInvalidated()和n
  9. Activity生命周期介绍
  10. ListView属性及方法android