Android:如何在 Android 编程中编辑数据库中的特定记录(使用 Ruby on Rails)
首先,我有一个使用 Ruby on Rails 创建的数据库。 我刚刚在我的 Android 应用程序中实现了插入功能(HTTPPost)并且它可以工作。 但我不知道如何从数据库中检索特定记录并将其插入到 Android 中的特定记录(如 RoR 中的编辑功能)
这是我的插入代码:
private void insertComment() { DefaultHttpClient 客户端 = new DefaultHttpClient();
HttpPost post = new HttpPost("http://10.10.3.87:3000/comments");
// Configure the form parameters
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("comment[content]", t_comment.getText().toString()));
nvps.add(new BasicNameValuePair("comment[id_account]", "1"));
nvps.add(new BasicNameValuePair("comment[id_place]", Integer.toString(position)));
try {
post.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
HttpResponse response = null;
try {
response = client.execute(post);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
HttpEntity entity = response.getEntity();
if (entity != null) {
try {
entity.consumeContent();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Toast.makeText(this, "Your post is successfully uploaded",
Toast.LENGTH_SHORT).show();
t_comment.setText("");
}
我确实尝试了很多方法,但它不起作用,并且需要很长时间才能与这段代码进行斗争。其实我真的不知道如何给HTTPPost指定RowID。
有人可以帮我吗? 提前致谢
At first,I have a database created by using Ruby on rails.
I just already implement insert function(HTTPPost) in my Android Application and it's work.
But I don't know how to retrieve specific record from my databases and insert it back to specific record in Android (Like edit function in RoR)
This is my insert code :
private void insertComment() {
DefaultHttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://10.10.3.87:3000/comments");
// Configure the form parameters
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("comment[content]", t_comment.getText().toString()));
nvps.add(new BasicNameValuePair("comment[id_account]", "1"));
nvps.add(new BasicNameValuePair("comment[id_place]", Integer.toString(position)));
try {
post.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
HttpResponse response = null;
try {
response = client.execute(post);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
HttpEntity entity = response.getEntity();
if (entity != null) {
try {
entity.consumeContent();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Toast.makeText(this, "Your post is successfully uploaded",
Toast.LENGTH_SHORT).show();
t_comment.setText("");
}
I really try many ways out but it doesn't work and it takes very long time to fight with this piece of code. Actually, I really don't know how to specify RowID to HTTPPost.
Can anyone help me please?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
感谢 fd 的精彩回答。 :-D
我读了你的评论,我有了新想法。
我尝试模仿 RoR 控制台中显示的请求。
在请求中,我们必须使用 PUT 方法附加 id 和数据,但我仅通过更改的 URL 请求修改了上面的插入代码
这意味着我们使用“action”=>“update”附加“id”=>“1”
此代码将调用 POST 方法而不是 put 方法,但它绝对有效!
非常感谢您的帮助 ^______________^
Thanks fd for your great answer. :-D
I read your comments and I got new idea.
I tried to imitate request that show in RoR console.
In the request, we have to attach the id along with data by using PUT method but I modified my insertcode above just only by chaged URL request
This mean we attach "id"=>"1" by using "action"=>"update"
This code will call POST method instead put method but it's absolutely work!!
THANKS A LOT FOR YOUR HELP ^______________^
我不确定 Android 方面,但从 Rails 的角度来看,我希望您希望将 HTTP GET 发送到 Rails 应用程序以获取资源 URL(例如: http://10.10.3.87:3000/comments/1234 用于 id 1234 的评论),确保您将接受标头设置为首选 XML 响应(您将需要为评论的显示操作定义一个 XML 视图才能正常工作)。
这应该会为您提供一个 XML 响应,您可以对其进行解码并在 Android 应用程序中显示。
类似的方法应该可以为您的评论建立索引,例如: GET 到 http://10.10.3.87:3000/comments (定义了 XML 视图)将为您提供评论索引,以便您可以选择一个来获取用于显示评论的正确 ID。
如果您的评论附加到其他一些“父”模型(例如,如果它们的范围仅限于帖子),这可能还不够,因为您还需要指定您有兴趣查看其评论的父模型。
我希望这有帮助!
I'm not sure about the Android side, but from the Rails perspective I would expect you want to send a HTTP GET to your Rails application for your resource URL (eg: http://10.10.3.87:3000/comments/1234 for comment with id 1234), ensuring you set your accept headers to prefer an XML response (you will need to define an XML view for the show action of your comment for this to work).
This should give you a response that is XML which you can decode and show in your Android app.
A similar approach should work to index your comments, for example: GET to http://10.10.3.87:3000/comments (with an XML view defined) would give you your index of comments so you can select one to get the correct id for showing the comment.
This may not be enough if your comments are attached to some other "parent" model (for example, if they are scoped on a post), since you will also need to specify the parent you are interested in seeing the comment for.
I hope that helps!