Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm trying to send a json object from my android activity to my asp.net web service, by the following code.

//This is Category.java
Java
JSONObject json=new JSONObject();
try
{
    CallSoap cs=new CallSoap();
    String name="Ramu";
    String username="ramu@gmail.com";
    String password="welcome";
    json.put("name", name);
    json.put("username",username);
    json.put("password", password);
    String resp=cs.demo(json);  // I need to send this json to my asp.net web server
    Log.d("Response from server",resp);
}
catch(Exception ex)
{
    ex.printStackTrace();
}

//This is callsoap.java
Java
public String demo(JSONObject a)
{
            public final String SOAP_ACTION5= "http://tempuri.org/demo";
            public final String OPERATION_NAME5=  "demo";
            public  final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/";
            public  final String SOAP_ADDRESS = "http://tn.selevanta.com/Convert.asmx"

    SoapObject request=new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME5);
    PropertyInfo pi=new PropertyInfo();
    pi.setName("a");
    pi.setValue(a);
    pi.setType(JSONObject.class);
    request.addProperty(pi);
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.dotNet = true;
    envelope.setOutputSoapObject(request);
    HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
    Object response=null;

    try
    {
        httpTransport.call(SOAP_ACTION5, envelope);
        response = envelope.getResponse();
    }
    catch(Exception ex)
    {
        response=ex.toString();
    }
    //JSONArray JSONArray = null;
    return response.toString();


}

I don't know what code should be written in my Convert.cs file in the server. I need to write a function in Convert.cs named demo. Then the json object from my android activity should be sent over to this demo function in Convert.cs. Upon receiving the json object at my Convert.cs file, I need to parse the json data from the json object received. So that the name, username, password from the json data should be saved to my sql server database. I know the coding for saving the data into the sql server database, but I don't know how to receive json object to my demo function and parse the json data available in the json object. I have also written another Person.cs file which contains the following data.
C#
public class Person
{

    public string name { get; set; }
    public string username { get; set; }
    public string password { get; set; }

}

This is a portion of code in Covert.cs file
C#
[System.Web.Services.WebMethod()]
 public String demo(//dont know how to receive the json object here)
 {
     // I don't know the Code for processing the json object received.

     return "success";
 }
Posted
Updated 14-Feb-14 5:25am
v3

1 solution

See this[^] article for instructions on how to post a JSON request to a webservice from Android.

I recommend using ASP .NET MVC to implement your webservice.  The default model binder will populate your model using the JSON properties sent by your Android (or any other type of) client.  See this CP article for a jump start: Working on JSON objects in jQuery and MVC[^]

/ravi
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900