/** * Opens an {@link HttpURLConnection} with parameters. * * @param request * @param listener * @return an open connection * @throws IOException */ private HttpURLConnection openConnection(WXRequest request, OnHttpListener listener) throws IOException { URL url = new URL(request.url); HttpURLConnection connection = createConnection(url); connection.setConnectTimeout(request.timeoutMs); connection.setReadTimeout(request.timeoutMs); connection.setUseCaches(false); connection.setDoInput(true); if (request.paramMap != null) { Set<String> keySets = request.paramMap.keySet(); for (String key : keySets) { connection.addRequestProperty(key, request.paramMap.get(key)); } } if ("POST".equals(request.method)) { connection.setRequestMethod("POST"); if (request.body != null) { if (listener != null) { listener.onHttpUploadProgress(0); } connection.setDoOutput(true); DataOutputStream out = new DataOutputStream(connection.getOutputStream()); // TODO big stream will cause OOM; Progress callback is meaningless out.write(request.body.getBytes()); out.close(); if (listener != null) { listener.onHttpUploadProgress(100); } } } else if (!TextUtils.isEmpty(request.method)) { connection.setRequestMethod(request.method); } else { connection.setRequestMethod("GET"); } return connection; }
private String readInputStream(InputStream inputStream, OnHttpListener listener) throws IOException { if (inputStream == null) { return null; } StringBuilder builder = new StringBuilder(); BufferedReader localBufferedReader = new BufferedReader(new InputStreamReader(inputStream)); char[] data = new char[2048]; int len; while ((len = localBufferedReader.read(data)) != -1) { builder.append(data, 0, len); if (listener != null) { listener.onHttpResponseProgress(builder.length()); } } localBufferedReader.close(); return builder.toString(); }
@Override public void sendRequest(final WXRequest request, final OnHttpListener listener) { if (listener != null) { listener.onHttpStart(); } execute( new Runnable() { @Override public void run() { WXResponse response = new WXResponse(); try { HttpURLConnection connection = openConnection(request, listener); Map<String, List<String>> headers = connection.getHeaderFields(); int responseCode = connection.getResponseCode(); if (listener != null) { listener.onHeadersReceived(responseCode, headers); } response.statusCode = String.valueOf(responseCode); if (responseCode >= 200 && responseCode <= 299) { response.originalData = readInputStreamAsBytes(connection.getInputStream(), listener); } else { response.errorMsg = readInputStream(connection.getErrorStream(), listener); } if (listener != null) { listener.onHttpFinish(response); } } catch (IOException e) { e.printStackTrace(); response.statusCode = "-1"; response.errorCode = "-1"; response.errorMsg = e.getMessage(); if (listener != null) { listener.onHttpFinish(response); } } } }); }
private byte[] readInputStreamAsBytes(InputStream inputStream, OnHttpListener listener) throws IOException { if (inputStream == null) { return null; } ByteArrayOutputStream buffer = new ByteArrayOutputStream(); int nRead; int readCount = 0; byte[] data = new byte[2048]; while ((nRead = inputStream.read(data, 0, data.length)) != -1) { buffer.write(data, 0, nRead); readCount += nRead; if (listener != null) { listener.onHttpResponseProgress(readCount); } } buffer.flush(); return buffer.toByteArray(); }