【问题标题】:Android check internet connection Toast displaying no internetAndroid 检查互联网连接 Toast 显示没有互联网
【发布时间】:2016-05-01 11:58:36
【问题描述】:

您好,我有一个小型应用程序,当它具有 Internet 连接但它没有 Internet 连接时,它只是显示一个 webview 我希望它显示一个 toast。目前,当我运行该应用程序时,即使有互联网连接,它也会显示一条吐司消息,说“没有互联网连接”,并且 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)

【问题讨论】:

  • 触发的异常是什么?
  • }catch (Exception e){ Toast.makeText(getContext(), "Webpage is not working", Toast.LENGTH_LONG).show(); }
  • 输入e.printStackTrace() 并在此处发布 logcat 输出。

标签: android webview timer toast android-internet


【解决方案1】:

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

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

【讨论】:

  • 将其更改为 getActivity().getApplicationContext() 因为我在一个片段上,但我仍然有同样的问题。
  • Toast.makeText(getActivity().getApplicationContext(), "网页正在运行", Toast.LENGTH_LONG).show();
  • 或者在你的片段中创建一个上下文并将其传递给 Toast。
【解决方案2】:

NetworkOnMainThreadException 在主 UI 线程上执行网络请求时触发。要找出问题,您必须使用 AsyncTask 并将 executeReq 代码放在其 doInBackground 方法中。

【讨论】:

  • 感谢您的建议,遗憾的是仍有问题:D
  • 您是否还在堆栈跟踪中将java.net.MalformedURLException: Protocol not found 视为异常?
  • 根据您的建议更改后它消失了
  • 好的,但是您是否收到正确的 toast 消息“网页正在运行”或新的不同异常?
  • 打开互联网后仍然显示“网页无法正常工作”
猜你喜欢
  • 2012-06-12
  • 1970-01-01
  • 2017-01-07
  • 2016-08-28
  • 1970-01-01
  • 2017-11-30
  • 2012-02-21
  • 2013-02-02
  • 2011-02-14
相关资源
最近更新 更多