Android:将网站设为字符串

时间:2014-07-25 23:20:35

标签: java android

我目前处于一个奇怪的境地:我的目标是获取网站的html并将其转换为String,以便我可以基本上阅读网站上的内容。但是,我的代码返回错误;我已经隔离了包含错误的代码行,但我无法使其工作。如下面的错误日志所示,强制转换的类型不正确。有谁知道如何修理它?谢谢!

public class FirstDisplay extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first_display);

        Button button1 = (Button) findViewById(R.id.deliver);
        final TextView textview1 = (TextView) findViewById(R.id.headline);

        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) {
                //textview1.setText(Html.fromHtml(result));
            }
        }

        button1.setOnClickListener (new View.OnClickListener() {
            @Override
            public void onClick (View v) {
                DownloadWebPageTask task = new DownloadWebPageTask();
                AsyncTask<String, Void, String> headline = task.execute(new String[] { "http://www.google.com" });
                //HERE IS THE LINE OF CODE THAT CONTAINS THE ERROR
                textview1.setText((CharSequence) headline);
            }

        });

    }

}

以下是错误日志:

07-25 18:14:42.359: W/dalvikvm(9268): threadid=1: thread exiting with uncaught exception (group=0x416dfda0)
07-25 18:14:42.359: E/AndroidRuntime(9268): FATAL EXCEPTION: main
07-25 18:14:42.359: E/AndroidRuntime(9268): Process: com.app.myfirstapp, PID: 9268
07-25 18:14:42.359: E/AndroidRuntime(9268): java.lang.ClassCastException: com.app.myfirstapp.MainDisplay$1DownloadWebPageTask cannot be cast to java.lang.CharSequence

2 个答案:

答案 0 :(得分:1)

你的问题在这里:

textview1.setText((CharSequence) headline);

标题是异步任务,而不是字符串(或其他标准顺序)。删除此行,并在onPostExecute中设置文本

答案 1 :(得分:0)

这是我的一些非常古老的代码,但我认为它曾经起作用:

static class scrapeIt extends AsyncTask<String, Void, String> {
        protected String doInBackground(String... urls) {
            HttpClient httpClient = new DefaultHttpClient();
            HttpGet get = new HttpGet(urls[0]);

            HttpResponse response;
            try {
                response = httpClient.execute(get);
                String bodyHtml = EntityUtils.toString(response.getEntity());
                return bodyHtml;
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return "Internal-Error: #0237";
        }
    }

我相信你的问题在于尝试将AsyncTask转换为&#34;字符串&#34;这是不允许的,你想要做的是获取AsyncTask生成的字符串并使用它。