Android检查互联网连接Toast显示没有互联网

时间:2016-05-01 11:58:36

标签: android webview timer toast android-internet

您好我有一个小应用程序,它只是在有互联网连接时显示webview但它没有互联网连接我希望它显示祝酒词。目前,当我运行应用程序时,即使有互联网连接并且webview将打开指定的网站,它也会显示一条“没有互联网连接”的Toast消息。如果没有互联网,我只希望看到这个祝酒词。我在下面包含了我的代码可以告诉我我做错了什么,我期待它只是一些小问题。

public class Facebook extends Fragment {

WebView facebookWebiew;

public Facebook() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
   //return inflater.inflate(R.layout.fragment_facebook, container, false);

    View v=inflater.inflate(R.layout.fragment_facebook, container, false);

    try{

        facebookWebiew = (WebView) v.findViewById(R.id.facebookWebview);
        facebookWebiew.loadUrl("https://www.facebook.com/");

        // Enable Javascript
        WebSettings webSettings = facebookWebiew.getSettings();
        webSettings.setJavaScriptEnabled(true);

        // Force links and redirects to open in the WebView instead of in a browser
        facebookWebiew.setWebViewClient(new WebViewClient());



        URL url = new URL("www.google.co.uk");
        executeReq(url);
        Toast.makeText(getContext(), "Webpage is working", Toast.LENGTH_LONG).show();

    }catch (Exception e){


        Toast.makeText(getContext(), "Webpage is  not working", Toast.LENGTH_LONG).show();

    }




    return v;


}


private void executeReq(URL urlObject)throws IOException{

    HttpURLConnection conn = null;
    conn = (HttpURLConnection) urlObject.openConnection();
    conn.setReadTimeout(30000); // milliseconds
    conn.setConnectTimeout(30000); // milliseconds
    conn.setRequestMethod("GET");
    conn.setDoInput(true);

    conn.connect();
    InputStream response = conn.getInputStream();
    Log.d("Response", response.toString());


}


}

清单

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

- 还使用权限访问网络

打印跟踪

 I/System.out: android.os.NetworkOnMainThreadException
 I/System.out:     at   android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
 I/System.out:     at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
 I/System.out:     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
 I/System.out:     at java.net.InetAddress.getAllByName(InetAddress.java:215)
 I/System.out:     at com.android.okhttp.HostResolver$1.getAllByName(HostResolver.java:29)
 I/System.out:     at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:232)
 I/System.out:     at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:124)
 I/System.out:     at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:272)
 I/System.out:     at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:211)
 I/System.out:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:382)
 I/System.out:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:106)
 I/System.out:     at com.android.okhttp.internal.http.DelegatingHttpsURLConnection.connect(DelegatingHttpsURLConnection.java:89)
 I/System.out:at com.android.okhttp.internal.http.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:25)
 I/System.out:at SocialMedia.Facebook.executeReq(Facebook.java:88)
 I/System.out:at SocialMedia.Facebook.onCreateView(Facebook.java:58)
 I/System.out:at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
 I/System.out:at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
 I/System.out:at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
 I/System.out:at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
 I/System.out:at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
 I/System.out:     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
 I/System.out:     at android.os.Handler.handleCallback(Handler.java:739)
 I/System.out:     at android.os.Handler.dispatchMessage(Handler.java:95)
 I/System.out:     at android.os.Looper.loop(Looper.java:135)
I/System.out:     at android.app.ActivityThread.main(ActivityThread.java:5254)
I/System.out:     at java.lang.reflect.Method.invoke(Native Method)
I/System.out:     at java.lang.reflect.Method.invoke(Method.java:372)
I/System.out:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
I/System.out:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

2 个答案:

答案 0 :(得分:0)

不要在这里使用getcontext(),使用getapplicationcontext()或getbasecontext()

Toast.maketext(getApplicationContext()," web page not working",Toast.LENGTH_LONG).show();

答案 1 :(得分:0)

在主UI线程上执行网络请求时会触发

NetworkOnMainThreadException。要找出问题,您必须使用AsyncTask并将executeReq代码放在其doInBackground方法中。