获取网站android的完整源代码

时间:2014-03-04 15:11:34

标签: android

我写了一个从网站上获取数据的应用程序。但我在这个应用程序中看不到图像链接等。我需要访问网站的完整源代码。换句话说,我需要在chrome中选择view page source选项时显示的源代码。以下是我的代码。

public class MainActivity extends Activity {
private TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textView = (TextView) findViewById(R.id.TextView01);
    readWebpage(textView);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
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://web.mit.edu/" });

    }
}`

在这个链接中,第一张图片是我得到的,第二张图片是我想要的。 http://imgur.com/7PYxC2y,0TphA5I#0

1 个答案:

答案 0 :(得分:0)

使用

textView.setText(result);

您可能正确地收到了下载的信息。你只是没有正确地呈现它。不要将其作为HTML呈现,就像纯文本一样。