【问题标题】:cannot connect to internet although i've the permission虽然我有权限但无法连接到互联网
【发布时间】:2012-06-20 16:52:20
【问题描述】:

我正在尝试制作一个浏览器应用,但问题是它无法连接到互联网

这是一个多选项卡,所以这些是代码:

主要活动:

public class TabTestActivity extends TabActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TabHost host=getTabHost();
        host.addTab(host.newTabSpec("two")
                .setIndicator("facebook")
                .setContent(new Intent(this,facebrowser.class)));
        host.addTab(host.newTabSpec("one")
                .setIndicator("Google")
                .setContent(new Intent(this,googlebrowser.class)));

    }
}

tab1 活动:

public class facebrowser extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        WebView browse = new WebView(this);
        setContentView(browse);
        browse.loadUrl("http:\\www.facebook.com");
    }
}

tab2 活动:

public class googlebrowser extends Activity{
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        WebView browse = new WebView(this);
        setContentView(browse);
        browse.loadUrl("http:\\www.google.com");
    }
}

xml 布局:

<?xml version="1.0" encoding="utf-8"?>
<TabHost
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />
            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
    </LinearLayout>
</TabHost>

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.x.TabBrowse"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".TabTestActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="googlebrowser" android:label="name">

        </activity>
        <activity android:name="facebrowser" android:label="name"></activity>
    </application>
</manifest>

页面告诉我:

网页不可用

http://www.facebook.com 上的网页可能暂时关闭,或者它可能已永久移至新网址。

以下是一些建议:

  • 检查以确保您的设备有信号和数据连接。
  • 稍后重新加载此网页。

查看来自 Google 的网页缓存副本

【问题讨论】:

    标签: android permissions webview


    【解决方案1】:

    而不是browse.loadUrl("http:\\www.facebook.com"); 利用 浏览.loadUrl("http://www.facebook.com");

    应该可以的

    对于您的第二个问题:您需要制作一个 webviewclient

    public class googlebrowser extends Activity{
    
        private class HelloWebViewClient extends WebViewClient {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;
            }
        }
    
    
        @Override
        public void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
            WebView browse = new WebView(this);
            setContentView(browse);
    
            browse.setWebViewClient(new HelloWebViewClient());
    
            browse.loadUrl("http://www.google.com");
        }
    }
    

    【讨论】:

    • 现在是另一个问题,它打开了不在我的 webview 中的常规浏览器
    【解决方案2】:

    您是在使用模拟器测试这一切吗?这可能没有互联网连接。这是一个有点普遍的问题。请参阅此处的其他 Stackoverflow 帖子 (http://stackoverflow.com/questions/2039964/how-to-connect-android-emulator-to-the-internet),这可能有助于您进行故障排除。

    【讨论】:

    • 不,我正在使用我的 wifi 连接在我的手机(华为 u8160)上测试它
    • 只要确保...您可以通过手机上的常规浏览器进行连接,对吗?
    • 是的,我可以从我的应用程序以外的所有应用程序正常连接
    猜你喜欢
    • 1970-01-01
    • 2015-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-14
    • 1970-01-01
    • 2018-03-19
    • 1970-01-01
    相关资源
    最近更新 更多