使用WCF REST和Android插入值的MySQL [英] Insert values in mySQL using wcf REST and Android
问题描述
我在C#写了一个REST Web服务。 Get请求工作正常,但插入,更新和删除不。
当我尝试从通过REST服务的Android平板电脑(客户端)一些值插入到一个MySQL数据库发生错误:不允许方法
I wrote a REST web service in C#. Get request works fine, but insert, update and delete not. When I try to insert some values from an Android tablet (client) via the REST service into a mySQL database an error occurs: Method not allowed
我从Android客户端JSON字符串发送到Web服务:
I send a JSON string from the android client to the web service:
在Android开发者工具(蚀):
in Android Developer Tool (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#中:
服务接口:
in C#: Service interface:
[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服务。但我不知道为什么。
这将是巨大的,如果有人能帮助我:)。
When I start the web service and try to insert the values via android app, nothing happens. I think, that the values from the app don't reach the web service. But I have no idea why. It would be great, if somebody can help me :).
在此先感谢
推荐答案
第一步是写一个C#单元测试演习通过Web服务的方法InsertUser。当运行这个测试,我认为你会发现你的UriTemplate不正确。如果你正在做一个POST,JSON对象将在请求的主体,而不是在URL中。我想尝试这样一个属性:
The first step would be to write a C# unit test that exercises the InsertUser method through the web service. When running this test I think you'd discover that your UriTemplate is incorrect. If you're making a POST, the json object will be in the body of the request, not in the url. I'd try an attribute like this:
[WebInvoke(Method = "POST", UriTemplate = "/users",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json]
您的Android网址是这样的(我认为):
Your android url would look like this (I think):
String url = IP + PORT + "/users";
另一件事是检查出是提琴手。当你的单元测试通过了,你可以检查成功请求。您 Android客户端还可以配置来使用Fiddler。
Another thing to check out would be Fiddler. When your unit test passes, you can inspect successful requests. Your android client can also be configured to use Fiddler.
这篇关于使用WCF REST和Android插入值的MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!