简体   繁体   English

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

[英]Android: How to edit specific record from database in Android Programming (Using Ruby on rails)

At first,I have a database created by using Ruby on rails. 首先,我有一个使用Ruby on Rails创建的数据库。 I just already implement insert function(HTTPPost) in my Android Application and it's work. 我已经在Android应用程序中实现了插入功能(HTTPPost),并且可以正常工作。 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) 但是我不知道如何从数据库中检索特定记录并将其插入到Android中的特定记录中(类似于RoR中的编辑功能)

This is my insert code : 这是我的插入代码:

private void insertComment() { DefaultHttpClient client = new DefaultHttpClient(); 私人无效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. 实际上,我真的不知道如何为HTTPPost指定RowID。

Can anyone help me please? 谁能帮我吗? Thanks in advance 提前致谢

Thanks fd for your great answer. 感谢fd的出色回答。 :-D :-D

I read your comments and I got new idea. 我读了您的评论后,有了新主意。

I tried to imitate request that show in RoR console. 我试图模仿在RoR控制台中显示的请求。

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 在请求中,我们必须使用PUT方法将ID与数据一起附加,但是我仅通过更改URL请求来修改了上面的插入代码

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

This mean we attach "id"=>"1" by using "action"=>"update" 这意味着我们通过使用“操作” =>“更新”来附加“ id” =>“ 1”

This code will call POST method instead put method but it's absolutely work!! 这段代码将调用POST方法而不是put方法,但是绝对可以!

THANKS A LOT FOR YOUR HELP ^______________^ 非常感谢您的帮助^ ______________ ^

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). 我不确定Android方面,但从Rails的角度来看,我希望您希望将HTTP GET发送到资源资源的Rails应用程序(例如: http : //10.10.3.87 : 3000/comments/1234 for ID为1234的注释),确保您将接受标头设置为首选XML响应(您需要为注释的show action定义一个XML视图,这样才能起作用)。

This should give you a response that is XML which you can decode and show in your Android app. 这应该给您一个XML响应,您可以将其解码并显示在Android应用程序中。

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. 一种类似的方法应该可以为您的评论建立索引,例如:GET到http://10.10.3.87:3000/comments (定义了XML视图)将为您提供评论索引,因此您可以选择一个以获得正确的ID用于显示评论。

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! 希望对您有所帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用哈希从数据库中的另一个记录创建的哈希在数据库中创建记录 - How to create a record in database using a hash which is created from another record in ruby on rails 使用 ruby​​ on rails 在单击按钮时从数据库中删除记录 - Delete record from database on clicking button using ruby on rails Ruby on Rails:如何插入具有从数据库中获取值的记录 - Ruby on Rails : How to insert record with fetching value from database 如何仅显示记录的编辑按钮并将数据从Rails中的编辑表单保存到数据库 - How to show edit button only for a record and save data to database from edit form in rails 使Android成为Rails上的数据库查询Ruby - Making Android an Database Query Ruby on Rails 使用Parse将通知从Ruby On Rails推送到Android和IOS用户 - Pushing notifications from Ruby On Rails to Android and IOS users using Parse 使用来自 ruby​​ on rails 的 firebase 的 Android 推送通知 - Android Push Notification using firebase from ruby on rails Ruby on Rails:如何通过用户界面中的按钮将特定/选择的记录从一个表(用户)发送到另一个表 - Ruby on Rails: How to send a specific/chosen record from one table (User) to another with a button from user interface Ruby on Rails没有将记录添加到数据库 - Ruby on Rails not adding record to database 如何防止Ruby on Rails Active记录从数据库中获取默认模型值? - How to prevent Ruby on Rails Active record from getting default model values from the database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM