Monday, July 23, 2012

Check URL is available or not



click here to download source code

package name   :  selva.ip

project Name    :   Ip

Version             :   1.5 ( Supports 1.5 and above versions)


main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

</LinearLayout>




 IpActivity.java



package selva.ip;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class IpActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
       
                 try
                   {

                           URL url = new URL("http://androidprogramz.in/" );
                         //URL url = new URL("http://www.nofoundwebsite.com/" );
                             executeReq(url);
                             Toast.makeText(getApplicationContext(), "Webpage is available!", Toast.LENGTH_SHORT).show();
        
                   }
                 catch(Exception e)
                     {
                             Toast.makeText(getApplicationContext(), "oops! webpage is not available!", Toast.LENGTH_SHORT).show();
                     }


    }
   
    private void executeReq(URL urlObject) throws IOException
    {
        HttpURLConnection conn = null;
        conn = (HttpURLConnection) urlObject.openConnection();
        conn.setReadTimeout(30000);//milliseconds
        conn.setConnectTimeout(3500);//milliseconds
        conn.setRequestMethod("GET");
        conn.setDoInput(true);

        // Start connect
        conn.connect();
        InputStream response =conn.getInputStream();
        Log.d("Response:", response.toString());
    }

}



Output :









change unavailable url  in  IpActivity.java

then run it.














































click here to download source code



2 comments: