Connection with internet...

      In this application we connect with internet when a button is pressed.It gives a response code of 200 in LogCat view,when properly connected with server and page is found.
So,we take a xml layout with a button in it...And here is the code below..


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
<Button android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Connect"></Button>
<ImageView android:id="@+id/ImageView01" android:layout_height="100dip" android:layout_width="100dip"></ImageView>
</LinearLayout>


Here we use two java class.In the first one named MyConnection we initialize the button and add the onClick method and inside we call the callNetwork method of another class Connection.The MyConnection class is defined below:



package com.android.connection;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MyConnection extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
               
        Button btn = (Button)findViewById(R.id.Button01);
        btn.setOnClickListener(new OnClickListener(){

            public void onClick(View v) {
            String url = "http://www.google.com";
           
            Connection conn = new Connection(url);
            conn.callNetwork();
   
               
            }
           
        });
              
    }
}


Now the Connection class where the callNetwork is defined..:

package com.android.connection;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.URLConnection;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.w3c.dom.Document;
import android.content.Context;



public class Connection {

    private boolean connecting;
    private String url = null;
    private String soapRequest = null;
    private String responsData = null;
    private String property = null;
    Context context;
   
    public Connection(String url) {
        
        this.url = url;
   
    }
   
    public boolean isConnected(){
        return connecting;
    }
    public static void callNetwork(){


        try {            

            URL url = new URL("http://www.google.com");
            URLConnection connection=url.openConnection();
            HttpURLConnection httpConnection=(HttpURLConnection)connection;
            int network_responsecode=httpConnection.getResponseCode();
            System.out.println("Response code is:"+ network_responsecode);
                     
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
   
   
    }
    }
 


Now the important thing is ,we have to add the user permission internet to the Android manifest by opening the permission tab before running the application.

In this application,when the button is pressed  "Response code is:200"  is shown in the logcat view.This means connection established successfully to the server.
*****************************************************************************************

No comments:

Post a Comment