- get 请求
public static String doGeg() {String result = "";BufferedReader reader;try {// 1.建立连接HttpURLConnection httpURLConnection = null;String url = "https://www.baidu.com";URL requestUrl = new URL(url);httpURLConnection = (HttpURLConnection) requestUrl.openConnection();httpURLConnection.setRequestMethod("GET");httpURLConnection.setConnectTimeout(5000);httpURLConnection.connect();// 2.获取二进制流InputStream inputStream = httpURLConnection.getInputStream();// 3.将二进制流包装reader = new BufferedReader(new InputStreamReader(inputStream));// 从Buffer reader中读取string字符串String line;StringBuilder builder = new StringBuilder();while ((line = reader.readLine()) != null) {builder.append(line);builder.append("\n");}if (builder.length() == 0) {return null;}result = builder.toString();} catch (Exception e) {Log.d("EEEE", "1");e.printStackTrace();}return result;}
- post 请求
public static boolean doPost(String utlStr) {HttpURLConnection urlConnection = null;OutputStream outputStream = null;boolean result = false;try {URL url = new URL(utlStr);// 1.打开连接urlConnection = (HttpURLConnection) url.openConnection();// 2.准备请求数据Map<String, String> paramMap = new HashMap<>();paramMap.put("userName", "zs");paramMap.put("pass", "123");String paramData = paramMapToString(paramMap);// 3.设置连接信息urlConnection.setRequestMethod("POST");urlConnection.setConnectTimeout(5000);urlConnection.setRequestProperty("Content-Length", String.valueOf(paramData.length()));// 设置conn可以向服务端输出的内容urlConnection.setDoOutput(true);// 4.获取输出流,并进行输出outputStream = urlConnection.getOutputStream();outputStream.write(paramData.getBytes());// 5.获取服务端的响应结果int code = urlConnection.getResponseCode();if (code == 200) {result = true;}} catch (Exception e) {e.printStackTrace();} finally {if (urlConnection != null) {urlConnection.disconnect();}if (outputStream != null) {try {outputStream.close();} catch (IOException e) {e.printStackTrace();}}}return result;}
将Map转为字符串
public static String paramMapToString(Map<String, String> paramMap) {StringBuilder sb = new StringBuilder();Set<Map.Entry<String, String>> entries = paramMap.entrySet();for (Map.Entry<String, String> entry : entries) {sb.append(entry.getKey()).append("=").append(entry.getValue()).append("&");}// 去掉最后一个&sb.deleteCharAt(sb.length() - 1);return sb.toString();}
处理JSON
public static void handleJson(String jsonStr) {try {JSONObject jsonObject = new JSONObject(jsonStr);String name = jsonObject.optString("name");Log.d("AAAA", name);} catch (JSONException e) {e.printStackTrace();}}
案例代码