从网页中提取文本仅提供URL

时间:2013-10-07 15:12:10

标签: java android html eclipse jsoup

请查看以下代码:

if (Intent.ACTION_SEND.equals(getIntent().getAction())) 
            {
                String text = getIntent().getStringExtra(Intent.EXTRA_TEXT);

                try
                {
                    Document doc = Jsoup.connect(text).get();
                    Log.i("DOC", doc.toString().toString());
                    Elements elementsHtml = doc.getElementsByTag("tr");  
                    ArrayList<String> temp1 = new ArrayList<String>();    

                    for(Element element: elementsHtml)
                    {

                        temp1.add( element.text());

                    }

                    //Add all the text to the edit text
                    for(int i=0;i<temp1.size();i++)
                    {
                        textField.append(temp1.get(i));
                    }

                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }


            }

此代码使用JSoup库和android代码。我需要从网页中提取所有文本,这就是上述代码的目的。但相反,它的作用是获取网页的网址并将其附加到文本字段,就是这样。

我需要所有文字。此代码的目标是“tr”。

为什么会这样?我该如何纠正?

更新

我改变了我的代码如下,现在我什么都没得到!

 //Get the web page text if called from Share-Via
            if (Intent.ACTION_SEND.equals(getIntent().getAction())) 
            {
                String text = getIntent().getStringExtra(Intent.EXTRA_STREAM);

                try
                {
                    Document doc = Jsoup.connect(text).get();
                    Log.i("DOC", doc.toString().toString());
                    Elements elementsHtml = doc.getElementsByTag("tr");  
                    ArrayList<String> temp1 = new ArrayList<String>();    

                    for(Element element: elementsHtml)
                    {

                        temp1.add( element.text());

                    }

                    //Add all the text to the edit text
                    for(int i=0;i<temp1.size();i++)
                    {
                        textField.append(temp1.get(i));
                    }

                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }


            }

如果有帮助

,这是我的清单文件代码(仅限必需部分)
<activity
            android:name="com.xxx.xxx.xxx"
            android:screenOrientation="portrait"
            android:label="@string/app_name" >

            <intent-filter>
                <action android:name="android.intent.action.SEND" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="text/plain"/>
                <data android:mimeType="image/*"/>
                <data android:mimeType="video/*"/>
            </intent-filter>

        </activity>

0 个答案:

没有答案