Wednesday, 28 March 2012

Creating A Webservice in php and Calling its using android

//Writing PHP WebService
<?php
// Pull in the NuSOAP code //download nusoap library Soap Library
require_once("lib/nusoap.php");
//making connection
require_once ('connection.php');
// Create the server instance
$server = new soap_server();
// Initialize WSDL support
$server->configureWSDL('hellowsdl', 'urn:hellowsdl');

//For Return Array of Data
$server->wsdl->addComplexType(
    'farmerdetails',
    'complexType',
    'struct',
    'all',
    '',
    array(
        'farmerid' => array('name' => 'farmerid', 'type' => 'xsd:string'),
        'farmername' => array('name' => 'farmername', 'type' => 'xsd:string'),
                                    'phoneno' => array('name' => 'phoneno', 'type' => 'xsd:string')
           
    )
);


// for returning Array of Array
$server->wsdl->addComplexType(
'Farmerdetailsarray',
'complexType',
'array',
'',
'SOAP-ENC:Array',
array(),
array(
array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'tns:farmerdetails[]')
),
'tns:farmerdetails'
);

//Register Webservice
$server->register('login',                // method name
    array('userid'=>'xsd:string', 'password'=>'xsd:string'),        // input parameters
    array('return' => 'tns:Farmerdetailsarray'),    // output parameters
    'urn:hellowsdl',                    // namespace
    'urn:hellowsdl#login',                // soapaction
    'rpc',                                // style
    'encoded',                            // use
    'Says get farmer preference'            // documentation
);
//Main login function
  function login($userid)
    {
      connection::connect();
        $LoginQuery="select organizationElement_verticalID from tbluserdetail where userid='".$userid."'";
        $LoginExecuteQuery=  mysql_query($LoginQuery) or die("Error in Query");
      
        while($data=mysql_fetch_array($LoginExecuteQuery, MYSQL_ASSOC))
        {
           
           $getfarmer="select farmerid, farmername from tblfarmer where organizationelementid='".$data[organizationElement_verticalID]."'";
            $getfarmerExecuteQuery=  mysql_query($getfarmer) or die("Error in query");
            $count=0;
             $farmer=array();
             while($farmerdetails=mysql_fetch_array($getfarmerExecuteQuery, MYSQL_ASSOC))
             {
                // $farmer[$count]=array("farmerid"=>$farmerdetails[farmerid],"farmername"=>$farmerdetails[farmername]);
                
                 $getfarmerphone="select farmerphone from tblfarmerphone where farmerid='".$farmerdetails[farmerid]."'";
                 $getfarmerphone_execute=mysql_query($getfarmerphone) or die("Error in phone get query");
                 while($farmerphone=  mysql_fetch_array($getfarmerphone_execute, MYSQL_ASSOC))
                 {
                      $farmer[$count]=array("farmerid"=>$farmerdetails[farmerid],"farmername"=>$farmerdetails[farmername],"phoneno"=>$farmerphone["farmerphone"]);
                  //$farmer[$count]=array("phone no"=>$farmerphone["farmerphone"]) ;  
                 }
                
               
              
                 $count++;
             }
            
         return $farmer;
            

         
            }
   
       
       
       
       
       
       
       
    }
   
 

// Use the request to (try to) invoke the service
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA);

?>




//Android Calling

import java.util.ArrayList;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import mla.FieldCoordinator.R.string;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.ParseException;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class Community extends Activity {
   
    //For message Printing
            private void showmessage(String msg)
            {
                Toast.makeText(getBaseContext(), msg, Toast.LENGTH_LONG).show();       
            }
    private String SOAP_ACTION = "urn:hellowsdl#getoptions";
     private String METHOD_NAME = "login";
     private  String NAMESPACE = "urn:hellowsdl";
     private String URL ="http://localhost/phphack/hellowsdl.php?wsdl";
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        try
        {
            Bundle bundle=getIntent().getExtras();
            String userid=bundle.getString("userid");
           
        super.onCreate(savedInstanceState);
        setContentView(R.layout.community);
        ArrayList<String> hi=new ArrayList<String>();
        String a;
        //hi.add("a");
        for(int i=65; i<=90;i++)
        {
            char c=(char)i;
            String c1=Character.toString(c);           
                hi.add(c1);
       
        }
       
        MyAdapter adapter=new MyAdapter(getApplicationContext(), hi,"alp");
        GridView gv=(GridView)findViewById(R.id.gridView1);
        gv.setAdapter(adapter);
       
       
        //GridView Cell Click Event
        gv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                TextView txt=(TextView)arg1;
                String val=txt.getText().toString();
                GridView gv=(GridView)findViewById(R.id.gridView2);
            ListAdapter la=    gv.getAdapter();
           
           
           
           
            ListView lv=new ListView(getBaseContext());
            lv.setAdapter(la);
            lv.setFilterText(val);
            lv.setTextFilterEnabled(true);
        CharSequence cs=lv.getTextFilter();
        String def="fdsf";
       
           
           
           
               
               
            }
           
        });
       
       
       
        // webservice calling
       
        SoapObject request=new SoapObject(NAMESPACE, METHOD_NAME);
       
        HttpTransportSE androidhttptransport=new HttpTransportSE(URL);
       
       
    //    AndroidHttpTransport androidhttptransport=new AndroidHttpTransport(URL);
        request.addProperty("userid", userid);
        //request.addProperty("password", "123456");
    //    request.addProperty("name2", 3);
       
        SoapSerializationEnvelope envelope= new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet=true;
        envelope.setOutputSoapObject(request);
       
       
       
            androidhttptransport.call(SOAP_ACTION, envelope);
           
       
            Object results=(Object)envelope.getResponse();
       
            String name1=results.toString();
            final ArrayList<String> arrayFrm=new ArrayList<String>();
            String[] data=    name1.split(",");
            String[] val;
            for(int i=0; i<data.length; i++)
            {
            String ss=data[i].replace("farmerdetails", "");
            String s=    ss.replace("[", "");
            String l=    s.replace("{", "");
            String ll=    l.replace("}", "");
            String ll1=    ll.replace("]", "");
                 val=ll1.split(";");
                 if(val.length>=2){
                 arrayFrm.add((val[0].replace("farmerid=", "")).trim());
                 arrayFrm.add((val[1].replace("farmername=", "")).trim());
                 arrayFrm.add((val[2].replace("phoneno=", "")).trim());}
                           
            }
           
            MyAdapter alldata=new MyAdapter(getApplicationContext(), arrayFrm,"grd");
            GridView alldaagrid=(GridView)findViewById(R.id.gridView2);
            alldaagrid.setAdapter(alldata);
           
            //gridview cell click
            alldaagrid.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3) {
                    Intent communitydashboard=new Intent(getBaseContext(), CommunityDashboard.class);   
                    communitydashboard.putExtra("id", arrayFrm.get(arg2-2));
                    communitydashboard.putExtra("name", arrayFrm.get(arg2-1));
                    communitydashboard.putExtra("pnum", arrayFrm.get(arg2));
                    startActivity(communitydashboard);
                   
                   
                }
            });
           
           
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
        //
       
       
       
       
       
       
       
       
       
        }
        catch(Exception e)
        {
            showmessage(e.getMessage().toString());
       
        }
    }
   
   
   
   
   
    //
   
    public class MyAdapter extends BaseAdapter {

        private Context context;
       
        private ArrayList<String> list; 
        private String type;
        public MyAdapter(Context context,ArrayList<String> list, String type) {
            this.context = context;
            this.list=list;
            this.type=type;
        }

        public int getCount() {
            return list.size(); 
        }

        public Object getItem(int position) {
            return list.get(position); 
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            TextView tv;
            if (convertView == null) {
                tv = new TextView(context);
                tv.setGravity(Gravity.CENTER);
               // tv.setLayoutParams(new GridView.LayoutParams(85, 85));
            }
            else {
                tv = (TextView) convertView;
            }
            if(type=="alp"){
            tv.setTextSize(25);}
            else
            {
                tv.setTextSize(18);
            }
            //tv.setTextColor(Color.BLACK);
                tv.setText(list.get(position));
              
            return tv;
        }
    }


}

Tuesday, 6 March 2012

Android Title Animaion

package popupshow.deepak;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewParent;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.TextView;
import android.widget.Toast;

public class PopupshowActivity extends Activity {
   
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
       
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
      //Get The Title
        View title=getWindow().findViewById(android.R.id.title);
            
        ViewParent parent=title.getParent();
         View parentview=(View)parent;
        parentview.setBackgroundColor(Color.rgb(100, 149, 237));
        Animation hyper=AnimationUtils.loadAnimation(this,android.R.anim.fade_out);

 //Creating animation object for transition of title 
        Animation isfromleft= new TranslateAnimation(Animation.RELATIVE_TO_SELF,0.0f,Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f);
        isfromleft.setDuration(10000);
        isfromleft.setInterpolator(new AccelerateInterpolator());
        isfromleft.setAnimationListener(animater);
       
       
          parentview.setAnimation(isfromleft);
   
              
       
       
    }
   
    private Animation.AnimationListener animater=new Animation.AnimationListener() {
       
        @Override
        public void onAnimationStart(Animation animation) {
            // TODO Auto-generated method stub
           
        }
       
        @Override
        public void onAnimationRepeat(Animation animation) {
           
           
        }
       
        @Override
        public void onAnimationEnd(Animation animation) {
            animation.start();
           
           
        }
    };   
   
}