【问题标题】:Insert values in mySQL using wcf REST and Android使用 wcf REST 和 Android 在 mySQL 中插入值
【发布时间】:2013-09-18 20:53:22
【问题描述】:

我用 C# 编写了一个 REST Web 服务。获取请求工作正常,但插入、更新和删除不行。 当我尝试通过 REST 服务将 Android 平板电脑(客户端)中的一些值插入 mySQL 数据库时,出现错误:不允许方法

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

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

String url = IP + PORT +"/RESTService/insert/";
            HttpClient client = new DefaultHttpClient();
            HttpResponse response;
            JSONObject json = new JSONObject();

                HttpPost post = new HttpPost(url);
                json.put("userName", name);
                json.put("userPassword", password);

                StringEntity se = new StringEntity(json.toString());
                se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,"application/json"));
                post.setEntity(se);
                response = client.execute(post);

在 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 应用程序插入值时,没有任何反应。我认为,来自应用程序的值不会到达网络服务。但我不知道为什么。 如果有人可以帮助我,那就太好了:)。

提前致谢

【问题讨论】:

    标签: c# android wcf rest


    【解决方案1】:

    第一步是编写一个 C# 单元测试,通过 Web 服务执行 InsertUser 方法。运行此测试时,我想您会发现您的 UriTemplate 不正确。如果您要进行 POST,则 json 对象将位于请求的正文中,而不是在 url 中。我会尝试这样的属性:

    [WebInvoke(Method = "POST", UriTemplate = "/users", 
        RequestFormat = WebMessageFormat.Json,       
        ResponseFormat = WebMessageFormat.Json]
    

    你的 android url 看起来像这样(我认为):

    String url = IP + PORT + "/users";
    

    要检查的另一件事是 Fiddler。当您的单元测试通过时,您可以检查成功的请求。你的android client can also be configured 使用 Fiddler。

    【讨论】:

    • 感谢您的快速回复。我将尝试 C# 单元测试。但是我有一些问题,在我的请求正文中,我的用户传输如下: {"userPassword":"test","userName":"testname"} 如何获取属性 userName 和 userPassword 及其值。什么样的参数(字符串,对象,...),在我的 Web 服务方法中我应该使用 public void InsertUser(string jsonObj) 或 public void InsertUser(object jsonObj) 将 JSONString 转换为我的 User 对象?
    • 您应该能够将您的 Web 服务签名更改为:public void InsertUser(User user) WCF 将自动处理用户的反序列化。
    • 完美,现在可以了;)。非常感谢服务签名提示。我将方法更改为:public void InsertUser(User user){string name = user.userName; string password = user.userPassword; ...}
    猜你喜欢
    • 2017-03-06
    • 2018-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-10
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多