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?

4
  • did you add Internet Permission in your manifest? Commented May 13, 2014 at 4:54
  • Likely you are getting an exception. If so, can you post the exception? Commented May 13, 2014 at 6:22
  • @Coderji thanks you for your response. I do have internet permission in my manifest: <uses-permission android:name="android.permission.INTERNET" > </uses-permission> Commented May 13, 2014 at 7: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. Commented May 13, 2014 at 7:21

1 Answer 1

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();    
Sign up to request clarification or add additional context in comments.

2 Comments

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); }
completely solved the problem, never mind the follow up question.! thanks again sir @JanBo!.. u saved my day!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.