wangguan
2020-12-29 377166293175b737d03e744e7d13500580ea78ea
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
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<String, String> 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<String, String> 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<String, String>参数
     * @param charset 编码类型 UTF-8
     * @return String
     * @throws UnsupportedEncodingException 不支持的编码类型
     */
    private static String urlParamsFormat(Map<String, String> params,
                                          String charset) throws UnsupportedEncodingException {
        final StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, String> 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();
    }
 
}