0

我正在尝试使用 android urlconnection 访问网页,即使使用 jsoup 解析它也成功了。但是,当我单击列表视图行时,该链接未在浏览器上打开。这是我的代码和 .xml。请帮我。

logcat

06-21 14:40:07.428: E/AndroidRuntime(10510): FATAL EXCEPTION: main
06-21 14:40:07.428: E/AndroidRuntime(10510): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=Full story: Daily Mail  }
06-21 14:40:07.428: E/AndroidRuntime(10510):    at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1622)
06-21 14:40:07.428: E/AndroidRuntime(10510):    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1417)
06-21 14:40:07.428: E/AndroidRuntime(10510):    at android.app.Activity.startActivityForResult(Activity.java:3370)
06-21 14:40:07.428: E/AndroidRuntime(10510):    at android.app.Activity.startActivityForResult(Activity.java:3331)
06-21 14:40:07.428: E/AndroidRuntime(10510):    at android.app.Activity.startActivity(Activity.java:3566)
06-21 14:40:07.428: E/AndroidRuntime(10510):    at android.app.Activity.startActivity(Activity.java:3534)
06-21 14:40:07.428: E/AndroidRuntime(10510):    at com.omeja.bbcsport.BBCActivity.onListItemClick(BBCActivity.java:234)
06-21 14:40:07.428: E/AndroidRuntime(10510):    at android.app.ListActivity$2.onItemClick(ListActivity.java:319)
06-21 14:40:07.428: E/AndroidRuntime(10510):    at android.widget.AdapterView.performItemClick(AdapterView.java:298)
06-21 14:40:07.428: E/AndroidRuntime(10510):    at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
06-21 14:40:07.428: E/AndroidRuntime(10510):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
06-21 14:40:07.428: E/AndroidRuntime(10510):    at android.widget.AbsListView$1.run(AbsListView.java:3423)
06-21 14:40:07.428: E/AndroidRuntime(10510):    at android.os.Handler.handleCallback(Handler.java:725)
06-21 14:40:07.428: E/AndroidRuntime(10510):    at android.os.Handler.dispatchMessage(Handler.java:92)
06-21 14:40:07.428: E/AndroidRuntime(10510):    at android.os.Looper.loop(Looper.java:137)
06-21 14:40:07.428: E/AndroidRuntime(10510):    at android.app.ActivityThread.main(ActivityThread.java:5041)
06-21 14:40:07.428: E/AndroidRuntime(10510):    at java.lang.reflect.Method.invokeNative(Native Method)
06-21 14:40:07.428: E/AndroidRuntime(10510):    at java.lang.reflect.Method.invoke(Method.java:511)
06-21 14:40:07.428: E/AndroidRuntime(10510):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-21 14:40:07.428: E/AndroidRuntime(10510):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-21 14:40:07.428: E/AndroidRuntime(10510):    at dalvik.system.NativeStart.main(Native Method)

代码

protected void onPostExecute(String result) {
            // TODO Auto-generated method stub

            super.onPostExecute(result);
            //
            //
            Document doc;
            try {
                // doc = Jsoup.connect(url1).get();
                // String title = doc.title();
                // Elements links = doc.getElementsByClass("external-link");
                doc = Jsoup.parse(result, url1);
                Elements links  = doc.select("h2 ~ p");
                // Elements links = doc.select("h2.cross-head");
                // Elements links = doc.select("a.external-link");
                // Elements media = doc.select("[src]");
                // Elements imports = doc.select("link[href]");
                //
                //
                for (Element link : links) {

                    Elements tableDatas = link.getElementsByTag("p");
                    //Elements tableDatas2 = link.getElementsByTag("a");
                    if (tableDatas.hasText()) {
                        String rowData = tableDatas.text();
                        // rowData2 = tableDatas2.text();
                        //if (!tableRowStrings.contains(rowData)) {
                            tableRowStrings.add(rowData);

                        //}

                    }
                    //

                }
                //
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                        BBCActivity.this, android.R.layout.simple_list_item_1,
                        tableRowStrings);
                setListAdapter(adapter);
                //

            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
        //

    }

    //
    @Override 
    public void onListItemClick(ListView l, View v, int position, long id) {
        // Do something when a list item is clicked

        //  String item = (String) getListAdapter().getItem(position);
          Uri uri = Uri.parse((String) tableRowStrings.get(position));
          //  Toast.makeText(this, item + " selected", Toast.LENGTH_LONG).show();
        // 
        Intent intent = new Intent(Intent.ACTION_VIEW,uri);
        startActivity(intent);
    }

显现

 <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <uses-feature />

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.omeja.bbcsport.BBCActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
4

2 回答 2

0

你应该称你为意图,如下所示:

Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(uri); // If you have the url, i.setData(Uri.parse(url));
startActivity(i);

让我知道你的进展。

于 2013-06-21T15:08:43.147 回答
0

像这样调用活动,并且 URL 必须始终以"http://" 开头,或者"https://"我使用这段代码。在某些情况下,URL 可能以"www".在这种情况下你会得到一个异常:

    String url = "http://www.example.com";
    Intent i = new Intent(Intent.ACTION_VIEW);
    i.setData(Uri.parse(url));
    startActivity(i);

您可以查看Intent.ACTION_VIEW的文档

于 2013-06-21T15:31:00.210 回答