package com.unity3d.player; import android.app.Activity; import android.util.Log; import android.widget.Toast; import com.google.gson.Gson; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import java.util.Map; public class UnityHttpUtils { private final static String PARAMETER_SEPARATOR = "&"; private final static String NAME_VALUE_SEPARATOR = "="; /** * Http GET 请求 * * @param urlStr * @param params * @return */ public static String httpGet(Activity activity,String urlStr, Map params) { String result = null; URL url = null; HttpURLConnection connection = null; InputStreamReader in = null; try { String paramsEncoded = ""; if (params != null) { paramsEncoded = urlParamsFormat(params, "UTF-8"); } String fullUrl = urlStr + "?" + paramsEncoded; Log.d("UnityHttp", "the fullAuthUrl is " + fullUrl); url = new URL(fullUrl); connection = (HttpURLConnection) url.openConnection(); connection.setReadTimeout(120000); connection.setConnectTimeout(60000); Log.d("UnityHttp", "open connection success"); if(connection.getResponseCode() == HttpURLConnection.HTTP_OK){ in = new InputStreamReader(connection.getInputStream(), "UTF-8"); BufferedReader bufferedReader = new BufferedReader(in); StringBuffer strBuffer = new StringBuffer(); String line = null; while ((line = bufferedReader.readLine()) != null) { strBuffer.append(line); } result = strBuffer.toString(); bufferedReader.close(); }else{ Toast.makeText(activity , "Net connect failed! error code:" + connection.getResponseCode() + "error msg:" + connection.getResponseMessage(), Toast.LENGTH_LONG).show(); } } catch (Exception e) { e.printStackTrace(); } finally { if (connection != null) { connection.disconnect(); } if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } return result; } /** * Http POST 请求 * * @param urlStr * @param params * @return */ public static String httpPost(Activity activity, String urlStr, Map params) { String result = null; URL url = null; HttpURLConnection connection = null; InputStreamReader in = null; try { String paramsEncoded = ""; if (params != null) { paramsEncoded = urlParamsFormat(params, "UTF-8"); } Log.d("UnityHttp", "the post url is " + urlStr + "\n paramsEncoded:" + paramsEncoded); url = new URL(urlStr); connection = (HttpURLConnection) url.openConnection(); connection.setDoInput(true); connection.setDoOutput(true); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setRequestProperty("Charset", "utf-8"); connection.setReadTimeout(120000); connection.setConnectTimeout(60000); DataOutputStream dop = new DataOutputStream( connection.getOutputStream()); dop.writeBytes(paramsEncoded); dop.flush(); dop.close(); if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { in = new InputStreamReader(connection.getInputStream(), "UTF-8"); BufferedReader bufferedReader = new BufferedReader(in); StringBuffer strBuffer = new StringBuffer(); String line = null; while ((line = bufferedReader.readLine()) != null) { strBuffer.append(line); } result = strBuffer.toString(); Log.d("UnityHttp", "result:" + result); bufferedReader.close(); } else { final int responseCode = connection.getResponseCode(); final String responseMessage = connection.getResponseMessage(); new Thread() { @Override public void run() { Toast.makeText( activity, "Net connect failed! error code:" + responseCode + "error msg:" + responseMessage, Toast.LENGTH_LONG).show(); } }.start(); } } catch (Exception e) { e.printStackTrace(); } finally { if (connection != null) { connection.disconnect(); } if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } return result; } /** * Returns a String that is suitable for use as an * application/x-www-form-urlencoded list of parameters in an HTTP PUT or * HTTP POST. * * @param params map参数 * @param charset 编码类型 UTF-8 * @return String * @throws UnsupportedEncodingException 不支持的编码类型 */ private static String urlParamsFormat(Map params, String charset) throws UnsupportedEncodingException { final StringBuilder sb = new StringBuilder(); for (Map.Entry entry : params.entrySet()) { final String encodedName = URLEncoder.encode(entry.getKey(), charset); final String encodedValue = URLEncoder.encode(entry.getValue(), charset); if (sb.length() > 0) { sb.append(PARAMETER_SEPARATOR); } sb.append(encodedName).append(NAME_VALUE_SEPARATOR) .append(encodedValue); } return sb.toString(); } }