0

Hello I have a small application which simply displays a webview when it has an internet connection but it doesn't have an internet connection I want it to display a toast. Currently when I run the application it will display a toast message saying "No internet connection" even though there is an internet connection and the webview will open to is designated website. I would only expect to see this toast message if there is no internet. I have included my code below could tell me what I have done wrong, I'm expecting its only something minor.

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());


}


}

Manifest

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

-- Also using permission Access network

Print Trace

 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)
haderss
  • 1
  • 3

2 Answers2

0

Don't use getcontext() here, use getapplicationcontext() or getbasecontext()

Toast.maketext(getApplicationContext()," web page not working",Toast.LENGTH_LONG).show();
Ramkumar.M
  • 681
  • 6
  • 21
0

NetworkOnMainThreadException is triggered when you execute network requests on the main UI thread. To figure out the issue you have to use an AsyncTask and place the executeReq code inside its doInBackground method.

Lino
  • 5,084
  • 3
  • 21
  • 39