Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用wcf REST和Android在mySQL中插入值_C#_Android_Wcf_Rest - Fatal编程技术网

C# 使用wcf REST和Android在mySQL中插入值

C# 使用wcf REST和Android在mySQL中插入值,c#,android,wcf,rest,C#,Android,Wcf,Rest,我用C#编写了一个REST web服务。Get请求可以正常工作,但插入、更新和删除不能正常工作。 当我试图通过REST服务将Android平板电脑(客户端)中的一些值插入mySQL数据库时,出现了一个错误:methodnotallowed 我将一个JSON字符串从android客户端发送到web服务: 在Android开发者工具(eclipse)中: 在C#中: 服务接口: [OperationContract] [WebInvoke(Method = "POST", UriTempla

我用C#编写了一个REST web服务。Get请求可以正常工作,但插入、更新和删除不能正常工作。 当我试图通过REST服务将Android平板电脑(客户端)中的一些值插入mySQL数据库时,出现了一个错误:methodnotallowed

我将一个JSON字符串从android客户端发送到web服务:

在Android开发者工具(eclipse)中:

在C#中: 服务接口:

[OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "/insert/{jsonObj}", 
        RequestFormat = WebMessageFormat.Json,       
        ResponseFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Bare)]
    void InsertUser(string jsonObj);
服务方法:

 public void InsertUser(string jsonObj)
    {
        string name = null;
        string password = null;
        DataContractJsonSerializer obj = new DataContractJsonSerializer(typeof(User));
        dynamic dynObj = JsonConvert.DeserializeObject(jsonObj);

        foreach (var data in dynObj.userObject)
        {
            name = data.userName;
            password = data.userPassword;
        }

       string sql = "INSERT INTO user (userName, userPassword) VALUES('" + name + "', '" + password +"';)";

       if (conn.State == ConnectionState.Closed)
       {
           conn.Open();
       }

       command = new MySqlCommand(sql, conn);
       command.ExecuteNonQuery();
       conn.Close();
       command.Dispose();
    }
我的用户:

    namespace RESTService
{
    [DataContract]
    public class User
    {
        [DataMember]
        public string userName { get; set; }
        [DataMember]
        public string userPassword { get; set; }

        public User(string name, string password)
        {
            this.userName = name;
            this.userPassword = password;
        }
    }
当我启动web服务并尝试通过android应用程序插入值时,什么都没有发生。我认为,应用程序中的值没有到达web服务。但我不知道为什么。 如果有人能帮我,那就太好了:)


提前感谢

第一步是编写一个C#单元测试,通过web服务练习InsertUser方法。运行此测试时,我认为您会发现您的URIMplate不正确。如果您正在发布帖子,json对象将位于请求主体中,而不是url中。我会尝试这样的属性:

[WebInvoke(Method = "POST", UriTemplate = "/users", 
    RequestFormat = WebMessageFormat.Json,       
    ResponseFormat = WebMessageFormat.Json]
你的android url应该是这样的(我想):


另一件要检查的东西是小提琴手。当单元测试通过时,可以检查成功的请求。您需要使用Fiddler。

感谢您的快速响应。我将尝试C#单元测试。但是我有一些问题,在我的请求主体中,我的用户被如下传输:{“userPassword”:“test”,“userName”:“testname”}我如何获得userName和userPassword属性及其值。在我的web服务方法中插入什么样的参数(字符串、对象…),我应该使用public void InsertUser(字符串jsonObj)或public void InsertUser(对象jsonObj)将JSONString转换为我的用户对象?您应该能够将web服务签名更改为:
public void InsertUser(用户)
WCF将自动处理用户的反序列化。很好,现在它可以工作了;)。非常感谢您提供的服务签名提示。我将方法更改为:
public void InsertUser(用户){string name=User.userName;string password=User.userPassword;..}
[WebInvoke(Method = "POST", UriTemplate = "/users", 
    RequestFormat = WebMessageFormat.Json,       
    ResponseFormat = WebMessageFormat.Json]
String url = IP + PORT + "/users";