@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == Activity.RESULT_OK && requestCode == STORY_REQUEST_CODE) { LogUtil.w("Latest---onActivityResult"); // 如果是已读的话,更新整个数据源 if (mClickItemPosition != -1) { View view = mStoryList.getChildAt(mClickItemPosition - mStoryList.getFirstVisiblePosition()); TextView txtView = (TextView) view.findViewById(R.id.title_story); txtView.setTextColor(Color.GRAY); } } }
@Override protected Document doInBackground(String... params) { Document document = null; try { LogUtil.e(params[0]); document = Jsoup.connect(params[0]).timeout(9000).post(); // Elements elements = document.getElementsByClass("ulTextlist_2 clear"); } catch (IOException e) { e.printStackTrace(); } if (document != null) { return document; } return null; }
@Override public long getExpiration() { if (connection == null) return -1L; long expiration = -1L; // from max-age String cacheControl = connection.getHeaderField("Cache-Control"); if (!TextUtils.isEmpty(cacheControl)) { StringTokenizer tok = new StringTokenizer(cacheControl, ","); while (tok.hasMoreTokens()) { String token = tok.nextToken().trim().toLowerCase(); if (token.startsWith("max-age")) { int eqIdx = token.indexOf('='); if (eqIdx > 0) { try { String value = token.substring(eqIdx + 1).trim(); long seconds = Long.parseLong(value); if (seconds > 0L) { expiration = System.currentTimeMillis() + seconds * 1000L; } } catch (Throwable ex) { LogUtil.e(ex.getMessage(), ex); } } break; } } } // from expires if (expiration <= 0L) { expiration = connection.getExpiration(); } if (expiration <= 0) { expiration = Long.MAX_VALUE; } return expiration; }
@Override public long getContentLength() { long result = 0; if (connection != null) { try { result = connection.getContentLength(); } catch (Throwable ex) { LogUtil.e(ex.getMessage(), ex); } if (result < 1) { try { result = this.getInputStream().available(); } catch (Throwable ignored) { } } } else { try { result = this.getInputStream().available(); } catch (Throwable ignored) { } } return result; }
@Override protected void onPostExecute(Document document) { super.onPostExecute(document); story_pull_list.onRefreshComplete(); File file = new File(Environment.getExternalStorageDirectory() + "/Latest_qbaobei.txt"); try { if (!file.exists()) { file.createNewFile(); } String str = document.toString(); FileWriter writer = new FileWriter(file.getAbsolutePath()); BufferedWriter bufferedWriter = new BufferedWriter(writer); bufferedWriter.write(str); bufferedWriter.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } if (document == null) { ToastAlone.show(R.string.load_fail_hint); return; } Element page = document.select("div.page").first(); if (page != null) { Elements children = page.children(); if (children.size() == 1 && "prev".equals(children.first().attr("class"))) { // 证明已经到了最后一页 isLastPage = true; ToastAlone.show(R.string.load_all_data); Log.i("cxm", "the last one"); return; } } progressbar.setVisibility(View.GONE); // 使用新的爬虫规则 Elements div_elements = document.select("div.news-list-ul"); if (div_elements != null) { Element div_fir = div_elements.first(); if (div_fir != null) { Elements div_children = div_fir.children(); if (div_children != null) { ArrayList<StoryBean> storyBeans = new ArrayList<StoryBean>(); for (Element element : div_children) { StoryBean bean = new StoryBean(); Element href_elem = element.select("a[href]").first(); String href_str = href_elem.attr("href"); Element img_elem = element.select("img[src]").first(); String img_str = Constans.defualt_pic; if (img_elem != null) { img_str = img_elem.attr("src"); } Element tit_element = element.select("p.tit").first(); String tit_str = tit_element.text(); LogUtil.v(tit_str + "---" + href_str + "---" + img_str); bean.setTitle(tit_str); bean.setImg(img_str); bean.setUrl(href_str); storyBeans.add(bean); } if (mLatestStart == 1) { mStoryAdapter.setData(storyBeans); } else { mStoryAdapter.addData(storyBeans); } } } } else { // 立马启动第二种解析方式 Elements ul_elements = document.select("ul.index-ul"); if (ul_elements != null) { Element ul_fir = ul_elements.first(); Elements ul_children = ul_fir.children(); if (ul_children != null) { ArrayList<StoryBean> storyBeans = new ArrayList<StoryBean>(); for (Element child : ul_children) { StoryBean bean = new StoryBean(); Element href_elem = child.select("a[href]").first(); Element img_elem = child.select("img[src]").first(); String title = href_elem.text(); String content_url = href_elem.attr("href"); String img_url = Constans.defualt_pic; if (img_elem != null) { img_url = img_elem.attr("src"); } bean.setTitle(title); bean.setUrl(content_url); bean.setImg(img_url); storyBeans.add(bean); } if (mLatestStart == 1) { mStoryAdapter.setData(storyBeans); } else { mStoryAdapter.addData(storyBeans); } } } } /*Elements elements = document.select("[class]"); for (Element element : elements) { if (element == null) { Log.v("cxm", "null"); } else { String className = element.className(); if ("index-ul".equals(className)) { Elements elements1 = element.select("li"); Log.v("cxm", "size=" + elements1.size()); ArrayList<StoryBean> storyBeans = new ArrayList<StoryBean>(); for (Element child : elements1) { StoryBean bean = new StoryBean(); Element href = child.select("[href]").first(); String name = href.text(); Element img = child.select("img[src]").first(); Log.w("cxm", "href=" + href.attr("href") + " ~~ name=" + name + "" + " ~~ img="); bean.setTitle(name); bean.setmContentUrl(href.attr("href")); if (null == img) { Log.e("cxm", "img = null"); bean.setPicUrl(""); } else { bean.setPicUrl(img.attr("src")); } storyBeans.add(bean); } if (mLatestStart == 1) { mStoryAdapter.setData(storyBeans); } else { mStoryAdapter.addData(storyBeans); } } } }*/ }
@Override @SuppressWarnings("unchecked") protected ResultType doBackground() throws Throwable { if (this.isCancelled()) { throw new Callback.CancelledException("cancelled before request"); } // 初始化请求参数 ResultType result = null; boolean retry = true; int retryCount = 0; Throwable exception = null; HttpRetryHandler retryHandler = new HttpRetryHandler(this.params.getMaxRetryCount()); request = initRequest(); if (this.isCancelled()) { throw new Callback.CancelledException("cancelled before request"); } // 检查缓存 Object cacheResult = null; if (cacheCallback != null && HttpMethod.permitsCache(params.getMethod())) { // 尝试从缓存获取结果, 并为请求头加入缓存控制参数. while (retry) { try { clearRawResult(); rawResult = this.request.loadResultFromCache(); break; } catch (Throwable ex) { LogUtil.w("load disk cache error", ex); exception = ex; retry = retryHandler.retryRequest(ex, ++retryCount, this.request); } } if (this.isCancelled()) { clearRawResult(); throw new Callback.CancelledException("cancelled before request"); } if (rawResult != null) { if (prepareCallback != null) { try { cacheResult = prepareCallback.prepare(rawResult); } catch (Throwable ex) { cacheResult = null; LogUtil.w("prepare disk cache error", ex); } finally { clearRawResult(); } } else { cacheResult = rawResult; } if (this.isCancelled()) { throw new Callback.CancelledException("cancelled before request"); } if (cacheResult != null) { // 同步等待是否信任缓存 this.update(FLAG_CACHE, cacheResult); while (trustCache == null) { synchronized (cacheLock) { try { cacheLock.wait(); } catch (Throwable ignored) { } } } // 处理完成 if (trustCache) { return null; } } } } if (trustCache == null) { trustCache = false; } if (cacheResult == null) { this.request.clearCacheHeader(); } // 发起请求 retry = true; while (retry) { try { if (this.isCancelled()) { throw new Callback.CancelledException("cancelled before request"); } // 由loader发起请求, 拿到结果. this.request.close(); // retry 前关闭上次请求 try { clearRawResult(); requestWorker = new RequestWorker(this.request, this.loadType); if (params.isCancelFast()) { requestWorker.start(); requestWorker.join(); } else { requestWorker.run(); } if (requestWorker.ex != null) { throw requestWorker.ex; } rawResult = requestWorker.result; } catch (Throwable ex) { clearRawResult(); if (this.isCancelled()) { throw new Callback.CancelledException("cancelled during request"); } else { throw ex; } } if (prepareCallback != null) { try { result = (ResultType) prepareCallback.prepare(rawResult); } finally { clearRawResult(); } } else { result = (ResultType) rawResult; } // 保存缓存 if (cacheCallback != null && HttpMethod.permitsCache(params.getMethod())) { this.request.save2Cache(); } retry = false; if (this.isCancelled()) { throw new Callback.CancelledException("cancelled after request"); } } catch (Throwable ex) { if (this.request.getResponseCode() == 304) { // disk cache is valid. return null; } else { exception = ex; retry = retryHandler.retryRequest(ex, ++retryCount, this.request); } } } if (exception != null && result == null && !trustCache) { throw exception; } return result; }
/** * invoke via Loader * * @throws IOException */ @Override @TargetApi(Build.VERSION_CODES.KITKAT) public void sendRequest() throws IOException { isLoading = false; URL url = new URL(queryUrl); { // init connection Proxy proxy = params.getProxy(); if (proxy != null) { connection = (HttpURLConnection) url.openConnection(proxy); } else { connection = (HttpURLConnection) url.openConnection(); } connection.setInstanceFollowRedirects(true); connection.setReadTimeout(params.getConnectTimeout()); connection.setConnectTimeout(params.getConnectTimeout()); if (connection instanceof HttpsURLConnection) { ((HttpsURLConnection) connection).setSSLSocketFactory(params.getSslSocketFactory()); } } { // add headers try { Map<String, List<String>> singleMap = COOKIE_MANAGER.get(url.toURI(), new HashMap<String, List<String>>(0)); List<String> cookies = singleMap.get("Cookie"); if (cookies != null) { connection.setRequestProperty("Cookie", TextUtils.join(";", cookies)); } } catch (Throwable ex) { LogUtil.e(ex.getMessage(), ex); } HashMap<String, String> headers = params.getHeaders(); if (headers != null) { for (Map.Entry<String, String> entry : headers.entrySet()) { String name = entry.getKey(); String value = entry.getValue(); if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(value)) { connection.setRequestProperty(name, value); } } } } { // write body HttpMethod method = params.getMethod(); connection.setRequestMethod(method.toString()); if (HttpMethod.permitsRequestBody(method)) { RequestBody body = params.getRequestBody(); if (body != null) { if (body instanceof ProgressBody) { ((ProgressBody) body).setProgressHandler(progressHandler); } String contentType = body.getContentType(); if (!TextUtils.isEmpty(contentType)) { connection.setRequestProperty("Content-Type", contentType); } long contentLength = body.getContentLength(); if (contentLength < 0) { connection.setChunkedStreamingMode(256 * 1024); } else { if (contentLength < Integer.MAX_VALUE) { connection.setFixedLengthStreamingMode((int) contentLength); } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { connection.setFixedLengthStreamingMode(contentLength); } else { connection.setChunkedStreamingMode(256 * 1024); } } connection.setRequestProperty("Content-Length", String.valueOf(contentLength)); connection.setDoOutput(true); body.writeTo(connection.getOutputStream()); } } } LogUtil.d(queryUrl); int code = connection.getResponseCode(); if (code >= 300) { HttpException httpException = new HttpException(code, connection.getResponseMessage()); try { httpException.setResult(IOUtil.readStr(connection.getInputStream(), params.getCharset())); } catch (Throwable ignored) { } throw httpException; } { // save cookies try { Map<String, List<String>> headers = connection.getHeaderFields(); if (headers != null) { COOKIE_MANAGER.put(url.toURI(), headers); } } catch (Throwable ex) { LogUtil.e(ex.getMessage(), ex); } } isLoading = true; }