2016-01-18 33 views
1

我想在我的庫中爲Volley的用法創建一個示例,並且字符串請求部分似乎沒有正常工作。代碼很簡單,我無法找到解決方案,爲什麼當我使用URL在線請求字符串時,它還包含HTML標記以及來自網站的HTML標記。使用Volley的字符串請求帶有不需要的HTML

如何在沒有HTML的情況下只拉取文本?

// <<<<<<<<<<<STRING REQUEST>>>>>>>> 
     // #1: A string from a URL. 
     String url ="http://httpbin.org/html"; 
     final TextView mTextView = (TextView) findViewById(R.id.stringView); 

     // #2: Request a string response from the provided URL. 
     StringRequest stringRequest = new StringRequest(Request.Method.GET, url, 
       new Response.Listener<String>() { 
        @Override 
        public void onResponse(String response) { 
         // Display the first 300 characters of the response string. 
         mTextView.setText(response.substring(0,300)); 
        } 
       }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       mTextView.setText("That didn't work!"); 
      } 
     }); 
     // #3: Add the request to the RequestQueue. 
     VolleySingleton.getInstance(this).addToRequestQueue(stringRequest); 

我顯示TextView字符串裏面,它應該是這樣的:

赫爾曼·梅爾維爾 - 白鯨

援用溫和,夏季涼爽的天氣是現在在 這些緯度統治,爲了準備特別活躍的追求 不久的將來預期,珀斯,乞丐,起泡的舊的鐵匠,沒有去除他的便攜式ge再次被抓住, 在爲Ahab的腿完成了他的供款工作後,仍然將 保留在甲板上,用前軀快速地箍住了ringbolts;現在幾乎不停地被頭人,魚販和隊員們爲他們做一些小小的工作;變造,或修理,或新 塑造自己的各種武器和船傢俱....

相反,它看起來是這樣的:

<!DOCTYPE html> <html> <head> </head> <body> 
 
     <h1>Herman Melville - Moby-Dick</h1> 
 

 
     <div> 
 
     <p> 
 
      Availing himself of the mild, summer-cool weather that now reigned in these latitudes, and in preparation for the peculiarly active pursuits shortly to be anticipated, Perth, the begrimed, blistered old blacksmith, had not removed his portable forge to the hold again, after concluding his contributory work for Ahab's leg, but still retained it on deck, fast lashed to ringbolts by the foremast; being now almost incessantly invoked by the headsmen, and harpooneers, and bowsmen to do some little job for them; altering, or repairing, or new shaping their various weapons and boat furniture.... 
 
     </p> 
 
     </div> 
 
    </body> 
 
</html>

+0

的網址返回的HTML。除了服務器返回的內容,您希望返回的http請求是什麼?你或者需要自己去除它,或者解析它。 – 323go

+0

那麼爲什麼Volley將其稱爲字符串請求?它沒有正確命名。但是我會找到解析它的方法。我只是確保我沒有問這個問題。我原以爲沃利是一個完整的解決方案,我想不是。 – Azurespot

+0

它的名字是正確的。 HTML標籤是字符串。 – 323go

回答

0

看起來像排球未完全得到StringRequest轉換成你可能想到的String格式。所以我們必須自己解析HTML。這可以使用Jsoup完成,只需很少的代碼。

要添加到Android Studio打開你的模塊設置,然後選擇左上角的「+」號,然後選擇「導入.JAR/.AAR包」,然後從你的文件系統中選擇你的jar下載。

確保將compile 'org.jsoup:jsoup:1.8.3'添加到您的build.gradle文件中。

改性代碼如下:

// <<<<<<<<<<<STRING REQUEST>>>>>>>> 
     // #1: A string from a URL. 
     String url ="http://httpbin.org/html"; 
     final TextView mTextView = (TextView) findViewById(R.id.stringView); 

     // #2: Request a string response from the provided URL. 
     StringRequest stringRequest = new StringRequest(Request.Method.GET, url, 
       new Response.Listener<String>() { 
        @Override 
        public void onResponse(String response) { 
         // Use Jsoup to parse HTML 
         Document doc = Jsoup.parse(response); 
         String parsedText = doc.body().text(); 
         // Display the first 300 characters of the response string. 
         mTextView.setText(parsedText.substring(0,300)); 
        } 
       }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       mTextView.setText("That didn't work!"); 
      } 
     });