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

CoDeGiRl
  • 174
  • 2
  • 16
  • Switch to `WCF` and after you have the generated client code you would have access to complex objects that are marked as `` in the `.svc` file. – OneFineDay Apr 27 '14 at 19:09
  • Thanks for commenting. Can you explain I dont know how to "switch to wcf" – CoDeGiRl Apr 27 '14 at 23:21
  • [What is WCF](http://msdn.microsoft.com/en-us/library/ms731082(v=vs.110).aspx) – OneFineDay Apr 27 '14 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? – CoDeGiRl Apr 27 '14 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. – OneFineDay Apr 28 '14 at 01:18
  • I am not 100% sure of the setup, since this is an Android project. In theory it should work though. – OneFineDay Apr 28 '14 at 01:19
  • @OneFineDay - In the OP's situation, they are better off to create a service **application** and host it (IIS, Windows Service, etc). The Android app will then consume the **hosted** WCF service as a client. You cannot add a .NET DLL (WCF Library in this case) to a Java project. – Tim May 10 '14 at 23:56

2 Answers2

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

Community
  • 1
  • 1
CoDeGiRl
  • 174
  • 2
  • 16
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??

CoDeGiRl
  • 174
  • 2
  • 16