-1

I need to consume a web service from android/eclipse by soap method.

ie., i have to give an input and show an appropriate result from the web service as per the users input.How to do this?

java class

public class Demo_webserviceActivity extends Activity
{ 
/** Called when the activity is first created. */

   private static String NAMESPACE = "http://tempuri.org/";
   private static String METHOD_NAME = "GetName";
    private static String SOAP_ACTION = "http://tempuri.org/GetName";
    private static String URL = "http://122.248.240.105:234/Service1.asmx";

   Button btnFar;
   EditText txtFar,txtCel;


   @Override
   public void onCreate(Bundle savedInstanceState)
   {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.main);

   btnFar = (Button)findViewById(R.id.btnFar);

   txtFar = (EditText)findViewById(R.id.txtFar);
   txtCel = (EditText)findViewById(R.id.txtCel);

   btnFar.setOnClickListener(new View.OnClickListener()
   {

   public void onClick(View v)
   {
     //Initialize soap request + add parameters
     SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);       

     //Use this to add parameters
     request.addProperty("Fahrenheit",txtFar.getText().toString());

     //Declare the version of the SOAP request
     SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

     envelope.setOutputSoapObject(request);
     envelope.dotNet = true;

     try 
     {
         HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

         //this is the actual part that will call the webservice
         androidHttpTransport.call(SOAP_ACTION, envelope);

         // Get the SoapResult from the envelope body.
         SoapObject result = (SoapObject)envelope.bodyIn;

         if(result != null)
         {
          //Get the first property and change the label text
           txtCel.setText(result.getProperty(0).toString());
         }
         else
         {
           Toast.makeText(getApplicationContext(), "No Response",Toast.LENGTH_SHORT).show();
         }
    }
    catch (Exception e)
    {
       e.printStackTrace();
       }
     }
       });
    } 
 }

web method

public class GetName 
{
public String GetName(String Fahrenheit){
    return(Fahrenheit);
}
}

logcat

06-12 17:40:00.322: W/InputManagerService(59): Starting input on non-focused client com.android.internal.view.IInputMethodClient$Stub$Proxy@44f60478 (uid=10040 pid=345)
 06-12 17:40:00.352: W/IInputConnectionWrapper(345): showStatusIcon on inactive InputConnection
 06-12 17:40:07.292: D/AndroidRuntime(352): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
06-12 17:40:07.292: D/AndroidRuntime(352): CheckJNI is ON
06-12 17:40:07.477: D/AndroidRuntime(352): --- registering native functions ---
06-12 17:40:08.062: D/AndroidRuntime(352): Shutting down VM
06-12 17:40:08.062: D/dalvikvm(352): Debugger has detached; object registry had 1 entries
06-12 17:40:08.102: I/AndroidRuntime(352): NOTE: attach of thread 'Binder Thread #3' failed
06-12 17:40:08.502: D/AndroidRuntime(360): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
06-12 17:40:08.502: D/AndroidRuntime(360): CheckJNI is ON
06-12 17:40:08.633: D/AndroidRuntime(360): --- registering native functions ---
06-12 17:40:09.152: I/ActivityManager(59): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.demo.webser/.Demo_webserviceActivity }
06-12 17:40:09.222: D/AndroidRuntime(360): Shutting down VM
06-12 17:40:09.222: D/dalvikvm(360): Debugger has detached; object registry had 1 entries
06-12 17:40:09.252: I/AndroidRuntime(360): NOTE: attach of thread 'Binder Thread #3' failed

Thanks a lot

Rohith
  • 991
  • 4
  • 18
  • 31

2 Answers2

4

You should use SoapPrimitive results = (SoapPrimitive)envelope.getResponse(); instead of SoapObject result = (SoapObject)envelope.bodyIn; because your webservice is returning a string not object.

himanshu
  • 1,990
  • 3
  • 18
  • 36
  • If i follow your answer. these lines showing error txtCel.setText(result.getProperty(0).toString()); what can i do – Rohith Jun 12 '12 at 11:48
  • remove the getProperty(0) and try with result.toString() – himanshu Jun 12 '12 at 11:52
  • It showing the same result as "Name not found"...i think may be problem on web method, was my web method source correct? – Rohith Jun 12 '12 at 12:01
  • have you add the [WebMethod] just before the GetName() in webservice? and post ur logcat data. – himanshu Jun 12 '12 at 12:03
  • yes i have added web method's source with logcat,please visit my question again... – Rohith Jun 12 '12 at 12:11
  • i dont find "Name not found"... error in logcat? r u getting this error on 'txtCel' edittext? – himanshu Jun 12 '12 at 12:16
  • "Name not found " is the answer right now i am getting on my edittext box...sorry its not an error. – Rohith Jun 12 '12 at 12:19
  • The following web service is i am working with http://122.248.240.105:234/Service1.asmx if you give "0001" as input on the method GetName you will get values as "Goundamani"..this is what i want – Rohith Jun 12 '12 at 12:24
  • Thats y i am asking you may be problem on my web method's source – Rohith Jun 12 '12 at 12:31
  • u r doing the wrong way with this webservice.change the following code request.addProperty("Fahrenheit",txtFar.getText().toString()); to request.addProperty("sName",txtFar.getText().toString()); because sName is parameter name GetName() receiving. – himanshu Jun 12 '12 at 12:36
  • if my answer helped you, you should accept it so that other ppl will find it easily – himanshu Jun 12 '12 at 12:50
  • surely..surely..sorry i forgotten that to do,due program ran – Rohith Jun 12 '12 at 12:54
  • http://122.248.240.105:234/Service1.asmx?op=GetName look here parameter name is sName and in android u have to pass the same parameter as u write in webservice method. – himanshu Jun 12 '12 at 13:01
  • Now i hope u understood this concept of android webservice call. – himanshu Jun 12 '12 at 13:03
  • can u help to answer this question ? http://stackoverflow.com/questions/26903238/how-to-parse-soap-response-when-child-tags-have-different-name-and-no-of-tag-are –  Nov 13 '14 at 07:26
1

You are going in right direction i.e The code you are written in android is right.Just make sure that soap action , Method , URl and namespace you are written is right. If you have any doubts regarding this or any other doubts you can write me.

Here basic android tutorial to access web service in androidbasic ksoap android tutorial

and for creating webservice in java use this Tutorial How to create java based web service

Sachin D
  • 1,370
  • 7
  • 29
  • 42
  • The code you are given is write.Just make sure you Deploy that service correctly.Please follow the link I given in my answer to check the web service.And I already created web service using that link and its working.If still you have problem then write me. – Sachin D Jun 12 '12 at 12:12