public String getParamString() throws Exception { final StringBuffer buffer = new StringBuffer(); final HashMap<String, Object> data = getData(); if (null == data) return ""; String and = ""; Log.d(LOG_TAG, "-------------- param start ---------------------------"); for (Entry<String, Object> entry : data.entrySet()) { String k = entry.getKey(); Object val = entry.getValue(); String v = val == null ? "" : String.valueOf(val); buffer.append(and); buffer.append(java.net.URLEncoder.encode(k, requestCharset)); buffer.append("="); buffer.append(java.net.URLEncoder.encode(v, requestCharset)); and = "&"; Log.d(LOG_TAG, k + " : " + v); } Log.d(LOG_TAG, "-------------- param end ---------------------------"); return buffer.toString(); }
@Override public boolean send() { boolean isSuccess = false; HttpURLConnection conn = null; ByteArrayOutputStream output = null; InputStream input = null; String strNetTypeName = ""; try { String sUrl = getUrl(); if (sUrl == null) { throw new Exception("you have not set request url"); } sUrl = sUrl + (getData() == null ? "" : ((sUrl.indexOf("?") > -1 ? (sUrl.endsWith("&") ? "" : "&") : "?") + getParamString())); Log.d(LOG_TAG, getId() + ":" + sUrl); URL url = new URL(sUrl); conn = getConnect(mContext, url); checkCacnelStatus(); conn.setConnectTimeout(getConnectTimeout()); conn.setReadTimeout(getGetDataTimeout()); final HashMap<String, String> header = getRequestHeader(); if (null != header) { final Iterator<Entry<String, String>> inerator = header.entrySet().iterator(); while (inerator.hasNext()) { Entry<String, String> entry = inerator.next(); conn.setRequestProperty(entry.getKey(), entry.getValue()); // Log.d("||" + entry.getKey(), entry.getValue()); } } if (mIfModifiedSince > 0) { conn.setIfModifiedSince(mIfModifiedSince); } requestStart = new Date().getTime(); checkCacnelStatus(); conn.connect(); checkCacnelStatus(); final int responseCode = conn.getResponseCode(); mHttpStatus = responseCode; if (responseCode == HttpStatus.SC_NOT_MODIFIED) { setResponseHeader(conn); requestEnd = new Date().getTime(); return true; } if (responseCode != HttpStatus.SC_OK) { Log.d(LOG_TAG, responseCode + "|||"); throw new java.net.SocketException("http status is not 200 or 304"); } if (conn.getContentType().contains("text/vnd.wap.wml") == true && tryCount < getTryLimit()) { tryCount++; return send(); } output = new ByteArrayOutputStream(); input = conn.getInputStream(); final int totalSize = conn.getContentLength(); int num = 0, downLoaded = 0; byte[] buf = new byte[BUFFERSIZE]; while (mIsNeedResponse && !checkCacnelStatus() && (num = input.read(buf)) != -1) { output.write(buf, 0, num); downLoaded += num; if (mOnProgressListener != null) { long now = ToolUtil.getCurrentTime(); if (now - lastProgressNotifyTime > PROGRESS_NOTIFY_OFFSET) { lastProgressNotifyTime = now; mOnProgressListener.onProgress(null, downLoaded, totalSize); } } } checkCacnelStatus(); requestEnd = ToolUtil.getCurrentTime(); checkCacnelStatus(); setResult(conn, output); buf = null; isSuccess = true; } catch (CancelException ex) { Log.e(LOG_TAG, "HttpStatus = " + mHttpStatus + "\n" + ToolUtil.getStackTraceString(ex)); } catch (java.net.ConnectException ex) { Log.e( LOG_TAG, "HttpStatus = " + mHttpStatus + "\n" + ToolUtil.getStackTraceString(ex) + " " + getId()); strNetTypeName = HttpUtil.getNetTypeName(); StatisticsEngine.trackEvent( IcsonApplication.app, "http_connect_exception", "network: " + strNetTypeName + ", HttpStatus = " + mHttpStatus); } catch (java.net.SocketException ex) { Log.e( LOG_TAG, "HttpStatus = " + mHttpStatus + "\n" + ToolUtil.getStackTraceString(ex) + " " + getId()); strNetTypeName = HttpUtil.getNetTypeName(); StatisticsEngine.trackEvent( IcsonApplication.app, "http_socket_exception", "network: " + strNetTypeName + ", HttpStatus = " + mHttpStatus); } catch (Exception ex) { Log.e(LOG_TAG, ToolUtil.getStackTraceString(ex)); strNetTypeName = HttpUtil.getNetTypeName(); StatisticsEngine.trackEvent( IcsonApplication.app, "http_other_exception", "network: " + strNetTypeName + ", HttpStatus = " + mHttpStatus); } finally { try { if (input != null) { input.close(); input = null; } if (output != null) { output.close(); output = null; } if (conn != null) { conn.disconnect(); conn = null; } } catch (Exception ex) { Log.e(LOG_TAG, ToolUtil.getStackTraceString(ex)); } } return isSuccess; }