TShopping

 找回密碼
 註冊
搜索
查看: 317|回復: 0

[教學] Android 如何抓取網頁內容(get website content)

[複製鏈接]
發表於 2016-2-6 00:59:11 | 顯示全部樓層 |閱讀模式
 
Push to Facebook Push to Plurk Push to Twitter 
當我想抓取網頁某個位置的的內容的時候
需要先讀取出該網頁內容
google找了許久
終於測試成功code
當然在貼上代碼之前
請先確認你的ANDROID XML網路連接程式代碼有加入

  1. public void onCreate(Bundle savedInstanceState) {
  2.     super.onCreate(savedInstanceState);
  3.     new HttpTask().execute("http://www.google.com");
  4. }


  5. public String getWebPage(String adresse) {

  6.     HttpClient httpClient = new DefaultHttpClient();
  7.     HttpGet httpGet = new HttpGet();

  8.     InputStream inputStream = null;

  9.     String response = null;

  10.     try {

  11.         URI uri = new URI(adresse);
  12.         httpGet.setURI(uri);

  13.         HttpResponse httpResponse = httpClient.execute(httpGet);
  14.         int statutCode = httpResponse.getStatusLine().getStatusCode();
  15.         int length = (int) httpResponse.getEntity().getContentLength();

  16.         Log.d("main", "HTTP GET: " + adresse);
  17.         Log.d("main", "HTTP StatutCode: " + statutCode);
  18.         Log.d("main", "HTTP Lenght: " + length + " bytes");

  19.         inputStream = httpResponse.getEntity().getContent();
  20.         Reader reader = new InputStreamReader(inputStream, "UTF-8");

  21.         int inChar;
  22.         StringBuffer stringBuffer = new StringBuffer();

  23.         while ((inChar = reader.read()) != -1) {
  24.             stringBuffer.append((char) inChar);
  25.         }

  26.         response = stringBuffer.toString();

  27.     } catch (ClientProtocolException e) {
  28.         Log.d("main", "HttpActivity.getPage() ClientProtocolException error", e);
  29.     } catch (IOException e) {
  30.         Log.d("main", "HttpActivity.getPage() IOException error", e);
  31.     } catch (URISyntaxException e) {
  32.         Log.d("main", "HttpActivity.getPage() URISyntaxException error", e);
  33.     } finally {
  34.         try {
  35.             if (inputStream != null)
  36.                 inputStream.close();

  37.         } catch (IOException e) {
  38.             Log.e(LOG_THREAD_ACTIVITY, "HttpActivity.getPage() IOException error lors de la fermeture des flux", e);
  39.         }
  40.     }

  41.     return response;
  42. }

  43. private class HttpTask extends AsyncTask<String, Integer, String> {

  44.     @Override
  45.     protected String doInBackground(String... urls) {
  46.         // TODO Auto-generated method stub
  47.         String response = getWebPage(urls[0]);
  48.         return response;
  49.     }

  50.     @Override
  51.     protected void onPostExecute(String response) {
  52.         Log.d("main", "HTTP RESPONSE" + response);
  53.         //tv1.setText(response);
  54.     }

  55.     @Override
  56.     protected void onPreExecute() {
  57.         // TODO Auto-generated method stub
  58.         super.onPreExecute();
  59.     }

  60.     @Override
  61.     protected void onProgressUpdate(Integer... values) {
  62.         // TODO Auto-generated method stub
  63.         super.onProgressUpdate(values);
  64.     }

  65. }
複製代碼
參考文章http://stackoverflow.com/questio ... site-in-android-app



 

臉書網友討論
您需要登錄後才可以回帖 登錄 | 註冊 |

本版積分規則



Archiver|手機版|小黑屋|免責聲明|TShopping

GMT+8, 2016-12-6 06:42 , Processed in 0.052461 second(s), 22 queries .

本論壇言論純屬發表者個人意見,與 TShopping綜合論壇 立場無關 如有意見侵犯了您的權益 請寫信聯絡我們。

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表