我正在尝试通过以下代码将我的android活动中的json对象发送到我的asp.net Web服务。
//This is Category.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
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();
}
我不知道应该在服务器的Convert.cs文件中写入什么代码。 我需要在Convert.cs中编写一个名为demo的函数。然后我的android活动中的json对象应该被发送到Convert.cs中的这个演示函数。在我的Convert.cs文件中收到json对象后,我需要解析收到的json对象的json数据。因此,应将json数据中的名称,用户名,密码保存到我的sql server数据库中。 我知道将数据保存到sql server数据库的编码,但我不知道如何将json对象接收到我的demo函数并解析json对象中可用的json数据。我还写了另一个Person.cs文件,其中包含以下数据。
public class Person
{
public string name { get; set; }
public string username { get; set; }
public string password { get; set; }
}
这是Covert.cs文件中的一部分代码
[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";
}
答案 0 :(得分:0)
您可以在此处使用JavaScriptSerializer为您完成所有艰苦的工作,请参阅下面的示例。
[System.Web.Services.WebMethod()]
public String demo(string personInfo)
{
var jss = new System.Web.Script.Serialization.JavaScriptSerializer();
var aPerson = jss.Deserialize<Person>(personInfo);