5

I want my Android program to check if file (e.g. index.php) exists in a remote server using its URL. i.e. if I click the button, it should check if a url exists. If it does, it should load that same URL otherwise display a message "File does not exist!"

I did something like this:

   private OnClickListener cameraListener9=new OnClickListener(){
    public void onClick(View v){

    idNo=editText.getText().toString();

    String URLName ="http://blahblah/kufpt/upload_test/"+ idNo + "/index.php";

    boolean bResponse = exists(URLName);
    if (bResponse==true)
    {
        Toast.makeText(MainActivity.this, "FILE EXISTS" , Toast.LENGTH_SHORT).show();
        WebView mWebView =(WebView)findViewById(R.id.webView);
        mWebView.loadUrl(URLName);

    }
    else
        Toast.makeText(MainActivity.this, "File does not exist!", Toast.LENGTH_SHORT).show();

    }
};

/* this is the function to be called to check if index.php file exists or has been created */ as suggested in Check if file exists on remote server using its URL

    public static boolean exists(String URLName){
    try {
      HttpURLConnection.setFollowRedirects(false);
      // note : you may also need
      //HttpURLConnection.setInstanceFollowRedirects(false)

      HttpURLConnection con =  (HttpURLConnection) new URL(URLName).openConnection();
      con.setRequestMethod("HEAD");
      return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
     }
    catch (Exception e) {
       e.printStackTrace();
       return false;
    }
  }

But, this always give me a false return value even if the file really does exists in the specified folder. Just a desperate attempt, I tried displaying the value of con.getResponsecode(), it always gives me a 0 value. Anybody could help me why is the output behaving like this?

Community
  • 1
  • 1
Joy
  • 75
  • 1
  • 7
  • did you add `Internet` Permission in your manifest? – Coderji May 13 '14 at 04:54
  • Likely you are getting an exception. If so, can you post the exception? – Henry May 13 '14 at 06:22
  • @Coderji thanks you for your response. I do have internet permission in my manifest: – Joy May 13 '14 at 07:13
  • @Henry No, I'm not getting any exception. I mean, I tried displaying the result of getResponseCode and HttpURLConnection.HTTP_OK and this always gives me 0 and 200 respectively, which made me believe that there was no exception. the thing is am not sure if this code really has established connection to the server. – Joy May 13 '14 at 07:21

1 Answers1

7

I believe you are doing this in your main thread. Thats the reason its not working, you cant perform network operations in your main thread.

Try putting the code in AsyncTask or Thread.

Edit 1: As a quick fix try wrapping your "file checking code" like this:

    new Thread() {

        public void run() {
        //your "file checking code" goes here like this
        //write your results to log cat, since you cant do Toast from threads without handlers also...

      try {
         HttpURLConnection.setFollowRedirects(false);
         // note : you may also need
         //HttpURLConnection.setInstanceFollowRedirects(false)

         HttpURLConnection con =  (HttpURLConnection) new URL(URLName).openConnection();
         con.setRequestMethod("HEAD");
         if( (con.getResponseCode() == HttpURLConnection.HTTP_OK) ) 
            log.d("FILE_EXISTS", "true");
         else 
            log.d("FILE_EXISTS", "false");
 }
              catch (Exception e) {
                e.printStackTrace();
               log.d("FILE_EXISTS", "false");;
           }
             }
      }.start();    
JanBo
  • 2,925
  • 3
  • 23
  • 32
  • SirJanBo, it works! Thank you so much... it gives me 200 if file does EXIST other 404 if not. But just a follow up question, is it possible to load the url inside this thread by using the WebView? It seems not working when i inserted this lines of code: if( (con.getResponseCode() == HttpURLConnection.HTTP_OK) ) { Log.d("FILE_EXISTS", "true"); WebView mWebView =(WebView)findViewById(R.id.webView); mWebView.loadUrl(URLName); } – Joy May 13 '14 at 08:38
  • completely solved the problem, never mind the follow up question.! thanks again sir @JanBo!.. u saved my day! – Joy May 13 '14 at 09:14