Android程序内使用HttpURLConnection请求Restful资源
来源:互联网 发布:php的未来发展 编辑:程序博客网 时间:2024/06/03 21:16
在Android项目使用HttpURLConnetion可以请求Restful资源,而其提供的setRequestProperty方法可以再请求头中加入其他所需信息,如Authorization等。代码如下:
package com.example.deng.firstapp.util;import android.util.Log;import org.json.JSONObject;import java.io.BufferedReader;import java.io.DataOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.UnsupportedEncodingException;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.SocketTimeoutException;import java.net.URL;/** * Created by deng on 2017/5/1. */public class HttpUtil { public static String executeGetMethod(String path, String authorization) { String response = ""; try { URL url = new URL(path); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestProperty("Authorization", "Basic " + authorization); connection.setRequestMethod("GET"); connection.setConnectTimeout(5000); connection.setReadTimeout(5000); connection.connect(); // 获得返回值 InputStream in = connection.getInputStream(); response = getResponse(in); Log.i("response", response); connection.disconnect(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return response; } public static String executePostMethod(String path, JSONObject param) throws SocketTimeoutException { String paramStr = param.toString(); String response = ""; try { URL url = new URL(path); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setDoInput(true); connection.setDoOutput(true); connection.setUseCaches(false); connection.setConnectTimeout(5000); connection.setReadTimeout(5000); connection.setRequestProperty("Charset", "UTF-8"); connection.setRequestProperty("Content-Length", String.valueOf(paramStr.length())); connection.setRequestProperty("Content-Type", "application/json"); connection.connect(); DataOutputStream out = new DataOutputStream(connection.getOutputStream()); // 写入请求的字符串 out.writeBytes(paramStr); out.flush(); out.close(); // 获得返回值 InputStream in = connection.getInputStream(); response = getResponse(in); connection.disconnect(); } catch (SocketTimeoutException e) { throw e; } catch (MalformedURLException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return response; } private static String getResponse(InputStream in) { BufferedReader reader = new BufferedReader(new InputStreamReader(in)); StringBuilder builder = new StringBuilder(); String line; try { while ((line = reader.readLine()) != null) { builder.append(line); } } catch (IOException e) { e.printStackTrace(); } finally { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } return builder.toString(); }}
除此之外,推荐两款调用Restful资源的工具:
1、Chrome插件Postman:
2、IntelliJ IDEA内工具,点击Tools->Test RESTful Web Service可以打开:
阅读全文
0 0
- Android程序内使用HttpURLConnection请求Restful资源
- Android 使用HttpUrlConnection处理请求
- Android使用HttpURLConnection请求网络资源
- Android 使用HttpURLConnection发送Post/Get请求
- Android使用HttpURLConnection发送Get请求
- Android中 使用HttpURLConnection实现GET请求
- Android上使用HttpURLConnection发送HTTP请求
- android使用HttpURLConnection发送Post/Get请求
- Android使用HttpURLConnection发送HTTP请求
- android中使用 HttpURLConnection 做网络请求
- HttpURLConnection来请求WEB资源
- Android HttpURLConnection post请求
- Android --HttpURLConnection请求失败?
- Android HttpUrlconnection数据请求
- 使用HttpURLconnection请求数据
- 使用HttpURLConnection请求数据
- 微信小程序 --- RESTful API 获取资源请求成功却无法获得资源
- Android中使用HttpURLConnection和HttpClient发送Http请求
- Java飞机大战0_1
- Servlet方法
- 字符串截取和逆序
- wait和notify的理解与使用
- Zookeeper的功能以及工作原理
- Android程序内使用HttpURLConnection请求Restful资源
- Vm虚拟机安装CentOS7-Minimal版本安装
- Varnish基础与示例
- 欢迎使用CSDN-markdown编辑器
- 【使用gpt分区安装Debian/Ubuntu后无法进入grub的解决方案】
- 04_ARM汇编自学笔记模型之ARM 状态下的寄存器组织
- RecyclerView嵌套RecyclerView滑动冲突
- Picasso图片加载框架源码解析
- 滑动窗口