Saturday 26 April 2014

Android WebService Calculator

Create WebService  Using NetBeans

i am using netbeans 7.4
open netbean ide
select-> new project->choose javaweb ->and select WebApplication
give ProjectName->give your ProjectLocation->select Server then Click finish button
then right click your project select new-> webservice->give webservice name

then give the code here

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.calcul;

import javax.jws.WebService;

import javax.jws.WebMethod;
import javax.jws.WebParam;

/**

 *
 * @author intel
 */
@WebService(serviceName = "calc")
public class calc {
 
 

    /**

     * This is a sample web service operation
     */
   /* @WebMethod(operationName = "hello")
    public String hello(@WebParam(name = "name") String txt) {
     
        System.out.println("nameeeeeeeeeeeee"+txt);
        return "Hello " + txt + " !";
    }*/

    /**

     * Web service operation
     * @param i
     * @param j
     * @param flag
     * @return
     */
    @WebMethod(operationName = "add")
    public int add(@WebParam(name= "name1") String k) {
        //TODO write your implementation code here:
     
        String ids = String.valueOf(k);

int aa=Integer.valueOf(ids.split("#")[0].trim());

int  bb=Integer.valueOf(ids.split("#")[1].trim());

        System.out.println("%%%%%%%%%%%%"+aa);

        System.out.println("%%%%%%%%%%%%"+bb);
        int s=aa+bb;
        return s;
    }
 
 
}

save ->cleanbuild->deploy->select webservice and right click select TestWebService
this is using netbean
------------------------------------------------------------------------------------------------------------

am using android studio here the code

layout->activity_main

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity"
    tools:ignore="MergeRootFrame" >

    <TextView

        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="Add"
        android:textSize="30dp"
        android:textStyle="bold"
        android:id="@+id/addtitle"
        android:layout_centerHorizontal="true"
        />
    <TextView
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="Value:"
        android:textSize="20dp"
        android:textStyle="bold"
        android:layout_below="@+id/addtitle"
        android:id="@+id/val1"/>
    <EditText
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_below="@+id/val1"
        android:hint="Enter a Value"
        android:singleLine="true"
        android:id="@+id/num1"/>
    <TextView
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_below="@+id/num1"
        android:text="Value:"
        android:textStyle="bold"
        android:textSize="20dp"
        android:id="@+id/val2"/>
    <EditText
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_below="@+id/val2"
        android:singleLine="true"
        android:hint="Enter a Value"
        android:id="@+id/num2"/>
    <Button
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_below="@+id/num2"
        android:text="Add"
        android:id="@+id/add"
        android:textStyle="bold"/>
    <TextView
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_below="@+id/add"
        android:textStyle="bold"
        android:id="@+id/result"
        android:textSize="20dp"/>
    <ProgressBar
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_toRightOf="@+id/addtitle"
        android:visibility="invisible"
        android:id="@+id/pgr"/>
    </RelativeLayout>

package->MainActivity

package com.demo.addsoap;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;

public class MainActivity extends Activity {

    Button b;
    TextView tv;
    EditText et,ed;
    ProgressBar pg;
    String val1,val2,valu;
    String displayText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        et=(EditText)findViewById(R.id.num1);
        ed=(EditText)findViewById(R.id.num2);
        tv=(TextView)findViewById(R.id.result);
        b=(Button)findViewById(R.id.add);
        pg=(ProgressBar)findViewById(R.id.pgr);

        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(et.getText().length()!=0&&ed.getText().length()!=0){
                    val1=et.getText().toString();
                    val2=ed.getText().toString();

                    AsyncCall task=new AsyncCall();
                    task.execute();

                }else {
                    tv.setText("Please Enter Values");
                }
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        switch (item.getItemId()) {
            case R.id.action_settings:
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
    }

    private class AsyncCall extends AsyncTask<String,Void,Void> {

        protected Void doInBackground(String...parms){

            displayText=WebService.involk(val1,"add",val2);
            return null;
        }

        protected void onPostExecute(Void result){
            tv.setText(displayText);

            pg.setVisibility(View.INVISIBLE);

        }
        protected void onPreExecute(){
            pg.setVisibility(View.VISIBLE);
        }

        protected void onProgressUpdate(Void...values){

        }
    }
}
create new java  class  WebService
package->WebService

package com.demo.addsoap;

/**
 * Created by intel on 26/4/14.
 */
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

/**
 * Created by intel on 26/4/14.
 */
public class WebService {

    private static String NAMESPACE="http://calcul.com/";

    private static String URL="http://192.168.1.23:8080/MySamp/calc?wsdl";

    private static String SOAP_ACTION="http://calcul.com/";

    public static String METHOD="add";

    public static String involk(String na,String webMethodName,String va){

        String resText=null;


        SoapObject request=new SoapObject(NAMESPACE,webMethodName);
        PropertyInfo sayHellopi=new PropertyInfo();


        String cc=na+"#"+va;
        sayHellopi.setName("name1");

       sayHellopi.setValue(cc);
        //sayHellopi.setValue(va);
        sayHellopi.setType(Integer.class);

        request.addProperty(sayHellopi);

        SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.setOutputSoapObject(request);

        HttpTransportSE httpTransportSE=new HttpTransportSE(URL);

        try {
            httpTransportSE.call(SOAP_ACTION+webMethodName+METHOD,envelope);
            SoapPrimitive primitive=(SoapPrimitive)envelope.getResponse();
            resText=primitive.toString();
            System.out.println("Connected.........................");

        }catch (Exception e){

            System.out.println("Error Value" + e);
            resText="Error Occur";
            e.printStackTrace();
        }





        return resText;

    }
}
AndroidMainfest.xml code is here

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.demo.addsoap"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="19" />
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.demo.addsoap.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>





if you want andriod zip fileCLICK
if you want java zip fileCLICK