public static ArrayList<ProvinceModel> getAreaModels() { if (null != areaModels) { return areaModels; } IPageCache cache = new IPageCache(); String str = cache.get(CacheKeyFactory.CACHE_FULL_DISTRICT); if (!TextUtils.isEmpty(str)) { try { if (null == parser) parser = new FullDistrictParser(); FullDistrictModel model = parser.parse(str); areaModels = model.getProvinceModels(); } catch (JSONException ex) { Log.e(LOG_TAG, ex); areaModels = null; } finally { parser = null; } } if (null == areaModels) { readFromRawFile(); } return areaModels; }
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(); }
private static void readFromRawFile() { Resources pResources = IcsonApplication.app.getResources(); InputStream pInputStream = pResources.openRawResource(R.raw.fulldistrict); try { byte[] aBytes = new byte[pInputStream.available()]; pInputStream.read(aBytes); String strRaw = new String(aBytes); if (strRaw != null && !strRaw.equals("")) { if (null == parser) parser = new FullDistrictParser(); FullDistrictModel model = parser.parse(strRaw); if (parser.isSuccess()) { areaModels = model.getProvinceModels(); IPageCache cache = new IPageCache(); cache.set(CacheKeyFactory.CACHE_FULL_DISTRICT, parser.getData(), 0); cache.set(CacheKeyFactory.CACHE_FULL_DISTRICT_MD5, model.getMD5Value(), 0); } } } catch (IOException ex) { Log.e(LOG_TAG, ex); areaModels = null; } catch (JSONException ex) { Log.e(LOG_TAG, ex); areaModels = null; } finally { if (null != pInputStream) { try { pInputStream.close(); } catch (IOException e) { e.printStackTrace(); } pInputStream = null; parser = null; } } }
private void initData() { IPageCache cache = new IPageCache(); String data = cache.get(getCacheKey()); if (data != null) { ArrayList<SearchFilterAttributeModel> models; try { models = mSearchFilterParser.parse(data); } catch (Exception ex) { Log.e(LOG_TAG, ex); models = null; } if (models != null) { requestFinish(models); return; } } showLoadingLayer(); String option = mSearchModel.getOption(); mSearchModel.setOption(null); String strUrl = SearchHelper.getSearchUrlParamter(mSearchModel); strUrl += "&dtype=attr"; Ajax ajax = ServiceConfig.getAjax(Config.URL_SEARCH_NEW, strUrl); if (null == ajax) return; mSearchModel.setOption(option); ajax.setParser(mSearchFilterParser); ajax.setOnSuccessListener(this); ajax.setOnErrorListener( new OnErrorListener() { @Override public void onError(Ajax ajax, Response response) { FilterActivity.this.onError(ajax, response); } }); ajax.send(); addAjax(ajax); }
@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; }