Android check 404 error

Question

I have a url link that downloads a m3u file, I want to check if the url exists and doesn't return 404 error so I have written the code below. It doesn't seem to work though. It shows me neither the toast.show nor the dialog. I just press the button and nothing happens. I really don't know what's wrong.

String host = "http://bailive.gr:6500/get.php?type=m3u&username=" + username + "&password=" + password;
boolean flag = checkurl(host);
if (flag == true){
   Toast.makeText(MainActivity.this, "File exists!", Toast.LENGTH_SHORT).show();
   //open player
}
else {
     showNotFoundDialog();}


private boolean checkurl(String host) {
    try {
        HttpURLConnection.setFollowRedirects(false);
        HttpURLConnection con = (HttpURLConnection) new URL(host).openConnection();
        con.setRequestMethod("GET");
        return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

That is the Run result

    W/System.err: android.os.NetworkOnMainThreadException
W/System.err:     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1148)
W/System.err:     at java.net.InetAddress.lookupHostByName(InetAddress.java:443)
W/System.err:     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:294)
W/System.err:     at java.net.InetAddress.getAllByName(InetAddress.java:214)
W/System.err:     at com.android.okhttp.internal.Dns$1.getAllByName(Dns.java:28)
W/System.err:     at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:216)
W/System.err:     at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:122)
W/System.err:     at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:292)
W/System.err:     at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
W/System.err:     at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:296)
W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:503)
W/System.err:     at com.churchtv.emmanouilvaresis.churchtv.MainActivity.checkurl(MainActivity.java:89)
W/System.err:     at com.churchtv.emmanouilvaresis.churchtv.MainActivity.onClick(MainActivity.java:67)
W/System.err:     at android.view.View.performClick(View.java:4457)
W/System.err:     at android.view.View$PerformClick.run(View.java:18491)
W/System.err:     at android.os.Handler.handleCallback(Handler.java:733)
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
W/System.err:     at android.os.Looper.loop(Looper.java:136)
W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5336)
W/System.err:     at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err:     at java.lang.reflect.Method.invoke(Method.java:515)
W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:871)
W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
W/System.err:     at dalvik.system.NativeStart.main(Native Method)
V/AudioManager: preDispatchKeyEvent keycode: 4  stream: -2147483648
V/AudioManager: preDispatchKeyEvent keycode: 4  stream: -2147483648
W/IInputConnectionWrapper: showStatusIcon on inactive InputConnection

Show source
| url   | android   2017-01-06 15:01 1 Answers

Answers to Android check 404 error ( 1 )

  1. 2017-01-06 16:01

    This exception is thrown when an application attempts to perform a networking operation on its main thread. Run your code in AsyncTask

    or

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy); 
    

    add this isde your oncreate method

    or

    Thread thread = new Thread(new Runnable() {
    
        @Override
        public void run() {
            try  {
    
    
                //Your code goes here
    
    
    
    
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
    
    thread.start(); 
    

Leave a reply to - Android check 404 error

◀ Go back