从Android发送json对象到ASP.net C# [英] Sending json object to ASP.net C# from Android

查看:72
本文介绍了从Android发送json对象到ASP.net C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过以下代码将我的android活动中的json对象发送到我的asp.net Web服务。



//这是类别.java

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

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();
}



//这是callsoap.java


//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文件。


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.

public class Person
{

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

}



这是Covert.cs文件中代码的一部分


This is a portion of code in Covert.cs file

[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";
 }

推荐答案

参见 this [ ^ ]文章,了解如何将JSON请求发布到Android的Web服务。



我建议使用ASP .NET MVC来实现您的网络服务。 默认模型绑定器将使用您的Android(或任何其他类型)客户端发送的JSON属性填充您的模型。 有关快速入门的信息,请参阅此CP文章: jQuery和MVC中的JSON对象 [ ^ ]



/ ravi
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


这篇关于从Android发送json对象到ASP.net C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆