1

I am trying to develop ASP.NET web application and also Android UI for the same application. I am new to android. I developed a simple screen that has a text box and button. Type something and clicking the button, saves the text in the database.

Now I am moving to complex functions. I need to implement a registration form. I will put more text boxes on the mobile screen. So I am planning to pass them to web-service in an object.

I created a web-service in C#, the method register takes an object Customer as parameter. Customer class has several fields like name, email, phone etc.

Please guide/ suggest an example to implement the functionality of passing object from androind to C# webservice and access the returned object values.

I am using KSoap2 for Android webservices. Thanks

1
  • any help for me here ? I use KSoap2 not Json. Commented Sep 27, 2012 at 18:45

2 Answers 2

1

You could implement a service in different ways. Some examples are:

You could send the data from your client (Android application) to your service in JSON. Inside this service you should implement something to convert this JSON notation back to a .net object. You can use JSON.net for this. More information about this, can be found here:

http://james.newtonking.com/projects/json-net.aspx

You should keep this in mind:

  1. Serialize data from your client (Android) to a string in JSON notation.
  2. Send the data to your service
  3. Deserialize the request to .NET object. (some frameworks do this for you)
  4. Do something with the .NET object and return a response to the client

Here are some examples for Android:

http://sarangasl.blogspot.nl/2011/10/android-web-service-access-tutorial.html http://www.youtube.com/watch?v=v9EowBVgwSo

Hope this helps you out.

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

Comments

0

Perhaps a concrete example is also useful. Assuming you can invoke AndroidHttpClient to do an HttpPost, you can leverage a simple asp.net mvc controller action to process the post (as xml) and return xml (or whatever) accordingly for you to parse.

For your asp.net mvc action, try something like this (routed to http://foo.com/Something/ProcessSomething):

...

[ ValidateInput(false)
]
public class SomethingController : Controller
{
  ...

  [ HttpPost
  ]
  public ActionResult ProcessSomething(SomeParameters Parameters, String Options)
  {
    ...

    String sProcessed = Parameters.Descriptor.ParamA + Parameters.Descriptor.ParamB;

    ...

    return this.Content
      ( String.Format
        ( "<result><processed>{0}</processed></result>"
        , sProcessed
        )
      , "text/xml"
      );
  }

  /// <summary>
  /// Description of a view model instance.
  /// </summary>

  [ XmlRoot("something")
  ]
  public class SomethingDescriptor
  {
    private String _ParamA = String.Empty;
    private String _ParamB = String.Empty;

    [ XmlElement("paramA")
    ]
    public String ParamA
    {
      set
      {
        this._ParamA = value;
      }
      get
      {
        return this._ParamA;
      }
    }

    [ XmlElement("paramB")
    ]
    public String ParamB
    {
      set
      {
        this._ParamB = value;
      }
      get
      {
        return this._ParamB;
      }
    }

  }

  /// <summary>
  /// View parameter deserializer.
  /// </summary>

  public class SomethingParameters
  {
    private SomethingDescriptor _Descriptor = new SomethingDescriptor();

    public SomethingDescriptor Descriptor
    {
      get
      {
        return this._Descriptor;
      }
    }

    public String Something
    {
      set
      {
        try
        {
          using (StringReader sR = new StringReader(value))
          {
            XmlSerializer xS = new XmlSerializer(typeof(SomethingDescriptor));

            this._Descriptor = xS.Deserialize(sR) as SomethingDescriptor;
          }
        }
        catch
        {
        }
      }
      get
      {
        return String.Empty;
      }
    }

  }

}

Your android app would post a variable such as "Something=<something><paramA>this is pA</paramA><paramB>this is paramB</paramB></something>" to http://foo.com/Something/ProcessSomething and get back xml that it can use to render something to the user.

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.