为什么我总是得到IOException?

时间:2013-10-09 05:24:24

标签: java android exception jsoup ioexception

请查看以下代码

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”按钮并选择此程序时,可以调用此类。此类的目的是从共享网页(任何页面)中提取文本(仅文本)并将其显示在此EditText中。不幸的是,这并没有给我任何输出。相反,它会生成 IOException 并打印网址。

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

doc.opencv.org

JSoup部分是从此帖子的选定答案中提取的 - How to extract text of paragraph from html using Jsoup?。在那种情况下,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?我做错了什么?

1 个答案:

答案 0 :(得分:1)

我发现了这个问题。我在Manifest文件中错过了Internet权限

<uses-permission android:name="android.permission.INTERNET"/>