Android:如何在 Android 编程中编辑数据库中的特定记录(使用 Ruby on Rails)

发布于 2024-08-16 20:41:46 字数 1700 浏览 5 评论 0原文

首先,我有一个使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

老旧海报 2024-08-23 20:41:46

感谢 fd 的精彩回答。 :-D

我读了你的评论,我有了新想法。

我尝试模仿 RoR 控制台中显示的请求。

在请求中,我们必须使用 PUT 方法附加 id 和数据,但我仅通过更改的 URL 请求修改了上面的插入代码

http://10.10.3.87:3000/comments/update/1

这意味着我们使用“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

http://10.10.3.87:3000/comments/update/1

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 ^______________^

岁月染过的梦 2024-08-23 20:41:46

我不确定 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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文