android中网络请求方式的总结
来源:互联网 发布:ubuntu安装 闪下划线 编辑:程序博客网 时间:2024/04/27 16:50
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-Type
- urlConn.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方式请求失败");
- }
- }
- android中网络请求方式的总结
- Android 网络请求的方式
- android中做网络请求的几种方式
- android 开发中接触的各种网络请求框架总结
- android 开发中接触的各种网络请求框架总结
- Android 网络请求方式
- Android网络请求方式
- android常用的网络请求方式
- Android的网络请求方式解析
- android 网络请求总结
- 网络请求的方式
- 【Android】【嘿!Java】Android工程、Java工程中通过HttpClient请求网络数据的不同方式
- React-Native中网络请求的总结
- React-Native中网络请求的总结
- React-Native中网络请求的总结
- Android总结 - 网络请求总结
- Android网络访问请求方式
- android 网络请求方式 例子
- Android开发(18)--NotiFication详解与使用
- linux下的双显卡切换
- [转]GCC内嵌汇编
- Debian命令行下网络参数设置的方法
- 关于中间人
- android中网络请求方式的总结
- 关于eclipse qemu调试内核,找不到…
- OnLoad与Page_Load的差异分析
- Make sure to have the zlib libs …
- emacs 的块编辑(列模式)
- jhost邀请码 2012 5月 - 2012 年6…
- 【转】MIPS寄存器介绍
- ubuntu 触控板 打字关闭
- [转]把手教你源代码制作龙芯64位系…