从网页获取文本到string

好吧,所以我是新来的Android,我想从网页的整个文字到一个string。 我发现了很多像这样的问题,但正如我所说我是新来的Android,我不知道如何在我的应用程序中使用它们。 我收到错误。 只有一个方法,我设法得到它的工作,它使用WebView和JavaScript,它是慢下来。 有人可以告诉我一些其他方式来做到这一点或如何加快WebView,因为我根本不用它来查看内容。 顺便说一句我已经添加了下面的代码来加速WebView

webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setBlockNetworkImage(true); webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(false); webView.getSettings().setPluginsEnabled(false); webView.getSettings().setSupportMultipleWindows(false); webView.getSettings().setSupportZoom(false); webView.getSettings().setSavePassword(false); webView.setVerticalScrollBarEnabled(false); webView.setHorizontalScrollBarEnabled(false); webView.getSettings().setAppCacheEnabled(false); webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE); 

并请如果你知道其他更好,更快的解决scheme比使用WebView请给我的主要活动的整个源代码或解释我应该写的地方,所以我不会得到错误..预先感谢!

用这个:

 public class ReadWebpageAsyncTask extends Activity { private TextView textView; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); textView = (TextView) findViewById(R.id.TextView01); } private class DownloadWebPageTask extends AsyncTask<String, Void, String> { @Override protected String doInBackground(String... urls) { String response = ""; for (String url : urls) { DefaultHttpClient client = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(url); try { HttpResponse execute = client.execute(httpGet); InputStream content = execute.getEntity().getContent(); BufferedReader buffer = new BufferedReader( new InputStreamReader(content)); String s = ""; while ((s = buffer.readLine()) != null) { response += s; } } catch (Exception e) { e.printStackTrace(); } } return response; } @Override protected void onPostExecute(String result) { textView.setText(Html.fromHtml(result)); } } public void readWebpage(View view) { DownloadWebPageTask task = new DownloadWebPageTask(); task.execute(new String[] { "http://www.google.com" }); } } 

main.xml中

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/readWebpage" android:onClick="readWebpage" android:text="Load Webpage"></Button> <TextView android:id="@+id/TextView01" android:layout_width="match_parent" android:layout_height="match_parent" android:text="Example Text"></TextView> </LinearLayout> 

这是我通常用来从互联网下载string的代码

 class RequestTask extends AsyncTask<String, String, String>{ @Override // username, password, message, mobile protected String doInBackground(String... url) { // constants int timeoutSocket = 5000; int timeoutConnection = 5000; HttpParams httpParameters = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); HttpClient client = new DefaultHttpClient(httpParameters); HttpGet httpget = new HttpGet(url[0]); try { HttpResponse getResponse = client.execute(httpget); final int statusCode = getResponse.getStatusLine().getStatusCode(); if(statusCode != HttpStatus.SC_OK) { Log.w("MyApp", "Download Error: " + statusCode + "| for URL: " + url); return null; } String line = ""; StringBuilder total = new StringBuilder(); HttpEntity getResponseEntity = getResponse.getEntity(); BufferedReader reader = new BufferedReader(new InputStreamReader(getResponseEntity.getContent())); while((line = reader.readLine()) != null) { total.append(line); } line = total.toString(); return line; } catch (Exception e) { Log.w("MyApp", "Download Exception : " + e.toString()); } return null; } @Override protected void onPostExecute(String result) { // do something with result } } 

你可以运行任务

new RequestTask().execute("http://www.your-get-url.com/");

由于您对查看内容根本不感兴趣,请尝试使用以下内容:

为了从一个URL获得你的源代码,你可以使用这个:

 HttpClient httpclient = new DefaultHttpClient(); // Create HTTP Client HttpGet httpget = new HttpGet("http://yoururl.com"); // Set the action you want to do HttpResponse response = httpclient.execute(httpget); // Executeit HttpEntity entity = response.getEntity(); InputStream is = entity.getContent(); // Create an InputStream with the response BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) // Read line by line sb.append(line + "\n"); String resString = sb.toString(); // Result is here is.close(); // Close the stream 

确保你在AsyncTask或者Thread中的主UI线程中运行。