0

Im trying to send a PersonalInfo object from android to vb.net using web services. I have the connection as i can send primitive types.

This is what i have so far....

This is the android activity

package com.msc.mynamespace;

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;

import com.msc.mynamespace.db.DatabaseHelper;
import com.msc.mynamespace.model.PersonalInfo;

import android.os.AsyncTask;
import android.os.Bundle;

import android.view.View;

import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MyActivity extends DashboardActivity {

    DatabaseHelper db ;

    TextView textView1;
    EditText editText1;
    Button button1;
    String test;

    final String NAMESPACE = "http://mywebnamespace.org/";
    final String URL = "http://localhost/MyWebService.asmx";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sync);
        db = new DatabaseHelper(this);
 }

    public void CallWebservice(View v) {


        this.SyncPersonalInfo();
    }


        //starting asynchronus task
     private class SyncPersonalInfoTask extends AsyncTask<String, Void, String> {

         @Override
         protected void onPreExecute() {
              //if you want, start progress dialog here
         }

         @Override
         protected String doInBackground(String... urls) {
             String webResponse = "";
            try{

              final String SOAP_ACTION = "http://mywebnamespace.org/SyncPersonalInfo";
              final String METHOD_NAME = "SyncPatientInfo";
              PersonalInfo pi=db.RetrievePersonalInfo();//this is PersonalInfo object that i get from the database

              SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 
              PropertyInfo fromProp =new PropertyInfo();
              fromProp.setName("PI");
              fromProp.setValue(pi);
              fromProp.setType(pi.getClass());
              request.addProperty(fromProp);



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

              //envelope.addMapping(NAMESPACE, "PersonalInfo",new PersonalInfo().getClass());

              HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

              androidHttpTransport.call(SOAP_ACTION, envelope);
              SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
              webResponse = response.toString();
           }
           catch(Exception e){

              Toast.makeText(getApplicationContext(),"Cannot access the web service"+e.toString(), Toast.LENGTH_LONG).show(); 
            }

             return webResponse;
        }
         @Override
         protected void onPostExecute(String result) {

                 Toast.makeText(getApplicationContext(),"Completed...", Toast.LENGTH_LONG).show();
              }
     }

        public void SyncPersonalInfo(View view) {
            SyncPersonalInfoTask task = new SyncPersonalInfoTask();

               //passes values for the urls string array 
               task.execute();
              }  
}

This is the vb.net part

    Imports System.Web
    Imports System.Web.Services
    Imports System.Web.Services.Protocols

<WebService(Namespace:="http://mywebnamespace.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class MyWebService
    Inherits System.Web.Services.WebService


    Public Class PersonalInfo
        Public fname As String
        Public lname As String
    End Class
    <WebMethod()> _
    Public Function SyncPatientInfo(//what to put here??) As String
       //how to manipulate the parameter??
    End Function



End Class

I am lost as to how to send the PersonalInfo object and how to receive it on the .net end and then manipulate it Please assist thanking you all in advance

7
  • Switch to WCF and after you have the generated client code you would have access to complex objects that are marked as <DataContract> in the .svc file. Commented Apr 27, 2014 at 19:09
  • Thanks for commenting. Can you explain I dont know how to "switch to wcf" Commented Apr 27, 2014 at 23:21
  • What is WCF Commented Apr 27, 2014 at 23:24
  • OneFineDay......do i need to create a WCF Service Library or WCF service application or can i use my existing .net web project? Commented Apr 27, 2014 at 23:44
  • You just need to add the service library to your project. Add the reference and once that is done it will make the client reference code. Commented Apr 28, 2014 at 1:18

2 Answers 2

1

I got through with the problem without using WCF. Basically the complex object had to implement kvmserializable. This stackoverflow QandA helped me. Ksoap: Cannot Serialize exception when passing user defined class as parameter to web method

Thanks for assistance OneFineDay

Sign up to request clarification or add additional context in comments.

2 Comments

Glad you got it figured out =)
I have another little problem. I got through with sending complex object using code below. how do I send a List of that complex object? Can anyone help
0

This is what i used to pass the PersonalInfo complex object that implements kvmserializable

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

        @Override
        protected void onPreExecute() {
            // if you want, start progress dialog here
        }

        @Override
        protected String doInBackground(String... urls) {
            String webResponse = "";
            try {

                final String SOAP_ACTION = "http://mywebnamespace.org/SyncPersonalInfo";
                final String METHOD_NAME = "SyncPersonalInfo";
                PersonalInfo pi = db.RetrievePersonalInfo();

                SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

                PropertyInfo fromProp = new PropertyInfo();
                fromProp.setName("PI");
                fromProp.setValue(pi);
                fromProp.setType(pi.getClass());
                request.addProperty(fromProp);

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

                envelope.addMapping(NAMESPACE, "PersonalInfo",new PersonalInfo().getClass());

                MarshalDouble md = new MarshalDouble();// for serializing double
                md.register(envelope);
                HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

                androidHttpTransport.call(SOAP_ACTION, envelope);
                SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
                webResponse = response.toString();
            } catch (Exception e) {
                webResponse = "Cannot access the web service" + e.toString();
            }

            return webResponse;
        }

        @Override
        protected void onPostExecute(String result) {
            Toast.makeText(getApplicationContext(), "Completed...",Toast.LENGTH_LONG).show();
        }
    }

The vb.net webservice that accepts the complex object is as follows

Public Class PersonalInfo
    Public ID As Integer
    Public FirstName As String
    Public LastName As String


End Class

     <WebMethod()> _
Public Function SyncPersonalInfo(ByVal PI As PersonalInfo) As String
   return PI.LastName

End Function

Now i want

<WebMethod()> _
Public Function SyncPersonalInfo(ByVal PersonalInfoList As List(Of PersonalInfo)) As String
  //iterate through PersonalInfoList

End Function

How do you send List to webservice??

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.