views:

328

answers:

2

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

A: 

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!

fd
BTW, with the latest versions of Rails I think that defining an XML view for an action is as simple as creating a file with the .xml extension in the appropriate place and filling it with XML content. For example, if you already have app/views/comments/show.html.erb you would create app/views/comments/show.xml and fill it with some XML like "<comment id="<%= @comment.id %>"><%= @comment.content %></comment>".
fd
A: 

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

kanakochii