0

请看下面的代码

TT.Java

public class TT extends Activity{

private EditText textField;

    private boolean isSpeaking = false;

    private AudioManager audioManager;

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


        textField = (EditText)findViewById(R.id.textField);
        if (Intent.ACTION_SEND.equals(getIntent().getAction())) 
    {
        String text = getIntent().getStringExtra(Intent.EXTRA_TEXT);
        Document doc;

         try {
              doc = Jsoup.connect("http://docs.opencv.org/doc/tutorials/introduction/windows_install/windows_install.html#windows-installation").get();
              Elements paragraphs = doc.select("p");
              for(Element p : paragraphs)
              {
                //System.out.println(p.text());
                textField.setText(p.text());
              }
            } 
            catch (IOException ex)
            {
                textField.setText(ex.getLocalizedMessage());
                Toast.makeText(this, ex.getLocalizedMessage(), Toast.LENGTH_LONG).show();

            } 


    }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.talk, menu);
        return true;
    }

    }

当您单击 android web 浏览器的“Share-Via”按钮并选择该程序时,可以调用此类。此类的目的是从共享网页(任何页面)中提取文本(仅文本)并将其显示在 this 中EditText。不幸的是,这没有给我任何输出。相反,它会生成一个IOException并打印 URL。

IOException我得到我以前从未见过的。我得到的例外是

doc.opencv.org

JSoup部分是从该线程的选定答案中提取的 -如何使用 Jsoup 从 html 中提取段落文本?. 在这种情况下,SO 成员说它正在工作!

以下是我如何将此类添加到Share-Via

AndroidManifest

<activity
        android:name="com.xxx.xxx.TT"
        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"/>
        </intent-filter>

    </activity>

下面是完整的堆栈跟踪

10-09 13:45:18.244: I/TextToSpeech.java(1325): initTts() successfully bound to service
10-09 13:45:18.464: E/IOException_Share(1325): IOException in share
10-09 13:45:18.464: E/IOException_Share(1325): java.net.UnknownHostException: docs.opencv.org
10-09 13:45:18.464: E/IOException_Share(1325):  at java.net.InetAddress.lookupHostByName(InetAddress.java:506)
10-09 13:45:18.464: E/IOException_Share(1325):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:294)
10-09 13:45:18.464: E/IOException_Share(1325):  at java.net.InetAddress.getAllByName(InetAddress.java:256)
10-09 13:45:18.464: E/IOException_Share(1325):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:69)
10-09 13:45:18.464: E/IOException_Share(1325):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:48)
10-09 13:45:18.464: E/IOException_Share(1325):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection$Address.connect(HttpConnection.java:322)
10-09 13:45:18.464: E/IOException_Share(1325):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:89)
10-09 13:45:18.464: E/IOException_Share(1325):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpConnection(HttpURLConnectionImpl.java:285)
10-09 13:45:18.464: E/IOException_Share(1325):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:267)
10-09 13:45:18.464: E/IOException_Share(1325):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:205)
10-09 13:45:18.464: E/IOException_Share(1325):  at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:425)
10-09 13:45:18.464: E/IOException_Share(1325):  at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:410)
10-09 13:45:18.464: E/IOException_Share(1325):  at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:164)
10-09 13:45:18.464: E/IOException_Share(1325):  at org.jsoup.helper.HttpConnection.get(HttpConnection.java:153)
10-09 13:45:18.464: E/IOException_Share(1325):  at com.xxx.xxx.TT.onCreate(Talk.java:71)
10-09 13:45:18.464: E/IOException_Share(1325):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-09 13:45:18.464: E/IOException_Share(1325):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
10-09 13:45:18.464: E/IOException_Share(1325):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
10-09 13:45:18.464: E/IOException_Share(1325):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-09 13:45:18.464: E/IOException_Share(1325):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
10-09 13:45:18.464: E/IOException_Share(1325):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-09 13:45:18.464: E/IOException_Share(1325):  at android.os.Looper.loop(Looper.java:123)
10-09 13:45:18.464: E/IOException_Share(1325):  at android.app.ActivityThread.main(ActivityThread.java:3683)
10-09 13:45:18.464: E/IOException_Share(1325):  at java.lang.reflect.Method.invokeNative(Native Method)
10-09 13:45:18.464: E/IOException_Share(1325):  at java.lang.reflect.Method.invoke(Method.java:507)
10-09 13:45:18.464: E/IOException_Share(1325):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-09 13:45:18.464: E/IOException_Share(1325):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-09 13:45:18.464: E/IOException_Share(1325):  at dalvik.system.NativeStart.main(Native Method)

我在JSoup这里使用过图书馆,但我没有这方面的经验。为什么我不断得到IOException?我究竟做错了什么?

4

1 回答 1

1

我发现了这个问题。我在清单文件中缺少 Internet 权限

<uses-permission android:name="android.permission.INTERNET"/>
于 2013-10-09T08:19:20.500 回答