安卓:得到网页的HTML作为字符串HttpClient的不工作

人气:1,030 发布:2022-09-11 标签: javascript java android httpclient

问题描述

我编程使用HttpClient的连接到一个网页(其目的是能够复制一些网页的HTML中的成字串)的应用程序。我试图通过使用HttpClient的连接实现这一点。这是code我用:

I am programming an app that uses HttpClient to connect to a web page (the aim is to be able to copy some of the HTML of a webpage into a String). I tried accomplishing this by using an HttpClient connection. This is the code I used:

public void getText() {
    final TextView contentView = (TextView) findViewById(R.id.textview);
   String url = "http://www.anandtech.com";
    /* An instance of this class will be registered as a JavaScript interface */ 
HttpClient client = new DefaultHttpClient();

HttpGet get = new HttpGet(url);
ResponseHandler<String> handler = new BasicResponseHandler();
try {
    String response = client.execute(get, handler);
    contentView.setText(response);
} catch (Exception e) {
    // TODO Auto-generated catch block
    contentView.setText(e.getMessage());
}

}

我找到了错误的根源去除该块的每个语句,然后如果仍然出现错误检测,发现该错误的client.execute(获取,处理程序)起源。我有权限使用互联网上AndroidManifest.xml中,我知道了网上的作品反正上的应用程序,因为一个web视图可以加载就好了。我怎样才能使这项工作?

I found the source of the error by removing each statement of this block and then testing if the error still occurs and found that the error originated from the client.execute(get, handler). I have permission to use internet on AndroidManifest.xml, and I know the internet works anyway on the app because a WebView can load just fine. How can I make this work?

另外,我试图通过注入的JavaScript到一个web视图的URL,用下面的code,以获得字符串:

Also, I tried to get the String by injecting javascript into a webview url, with the following code:

MyJavaScriptInterface.class       类MyJavaScriptInterface         {             私人TextView的内容查看;

MyJavaScriptInterface.class class MyJavaScriptInterface { private TextView contentView;

        public MyJavaScriptInterface(TextView aContentView)
        {
            contentView = aContentView;
        }
    }

MainActivity.java        @燮pressWarnings(未使用)

MainActivity.java @SuppressWarnings("unused")

        public void processContent(String aContent) 
        { 
            final String content = aContent;
            contentView.post(new Runnable() 
            {    
                public void run() 
                {          
                    contentView.setText(content);        
                }     
            });
        } 
    } 

    webView.getSettings().setJavaScriptEnabled(true); 
    webView.addJavascriptInterface(new MyJavaScriptInterface(contentView),"INTERFACE"); 
    webView.setWebViewClient(new WebViewClient() { 
        @Override 
        public void onPageFinished(WebView view, String url) {
        webView.loadUrl("javascript:window.INTERFACE.processContent(
        document.getElementsByTagName('p')[0].innerHTML);");
        } 
    }); 

    webView.loadUrl("http://tabshowdown.blogspot.com");

无论是类和processContent()方法在活动的无效的onCreate方法,FIY被定义。在这种情况下,问题是JavaScript是不启动processContent()方法,由于一个。如果我尝试初始化任何其他JavaScript命令,例如:文件撰写(),web视图的反应,所以JavaScript是没有问题的。如果我尝试启动MyJavaScriptInterface和其他地方的脚本processContent()方法,它工作得很好。所以,在我看来好像JavaScript是有麻烦启动方法(即使其他用户报告使用此方法)。

Both the class and the processContent() method were defined in the Activity's void onCreate method, FIY. In this case the problem is the javascript isn't initiating the processContent() method, since a. If I try to initialize any other javascript command, e.g. document.write(), the webview reacts, so javascript isn't the problem. If I try to initiate the MyJavaScriptInterface and the processContent() method from anywhere else on the script, it works just fine. So it appears to me as if the javascript is having trouble initiating the method (even though other users reported this method to work).

有人可以帮助我获得了上述两种方法的工作? 在此先感谢

Can someone help me get either of these methods to work? Thanks in advance

推荐答案

这应该工作:

public class MainActivity extends Activity{
    private TextView contentView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(your layout);
        contentView = (TextView) findViewById(R.id.textview);
        DownloadTask task = new DownloadTask();
        task.execute(url);
    }
    private class DownloadTask extends AsyncTask<String, Void, String>{

            @Override
            protected String doInBackground(String... urls) {
                HttpResponse response = null;
                HttpGet httpGet = null;
                HttpClient mHttpClient = null;
                String s = "";

                try {
                    if(mHttpClient == null){
                        mHttpClient = new DefaultHttpClient();
                    }


                    httpGet = new HttpGet(urls[0]);


                    response = mHttpClient.execute(httpGet);
                    s = EntityUtils.toString(response.getEntity(), "UTF-8");


                } catch (IOException e) {
                    e.printStackTrace();
                } 
                return s;
            }

            @Override
            protected void onPostExecute(String result){
                contentView.setText(result);

            }
        }
}

215