在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可以打开: