-1

Hey so I am pretty new to this android dev world and am trying to figure out a few things...

First off the end goal is to be able to create a form on android and to click on a submit button that should essentially send over the data just like a form on a website with AJAX doing the POST and GET response.

I have created the form and the button in the layout and have called in the java file a onClickListener that has the input text from the user, but I do not know how to send the data as a json to my node.js servers.

Here is my code:

//button for posting details
Button postWardrobe = (Button) findViewById(R.id.postButton);

final EditText nameField = (EditText) findViewById(R.id.nameFieldWardrobeScreen);

 postWardrobe.setOnClickListener(new OnClickListener() {

        private Editable Data;

        @Override
        public void onClick(View v) {

            //below should send data over
            Data = nameField.getText();

        }

});

I added only the code that is currently being worked with as the submit form... now I read some thing about http Connection but I am not sure how to implement it exactly or how to send it over to the server as it does not seem to link to it in any way... I want it to post to my localhost and also in the future to the server...

Also, I am not sure if I should be using sockets instead or just http... any advice and answer to sending the data over would be great, thanks.

Lion789
  • 4,102
  • 12
  • 50
  • 89
  • this is totally a solved problem on stack overflow. http://stackoverflow.com/questions/6218143/android-post-json-using-http – Jon Jan 16 '14 at 00:59
  • No need to negative the question... that answer was from 2011 and it does not provide libraries and shows a lot of code – Lion789 Jan 16 '14 at 18:45

3 Answers3

1

You need to build a JSON object, then send it over HTTP.

Check these pages and the related classes mentioned there.

create json in android

Make an HTTP request with android

How to send a JSON object over Request with Android?

See also:

http://www.vogella.com/tutorials/AndroidJSON/article.html

Community
  • 1
  • 1
peter.petrov
  • 34,474
  • 11
  • 63
  • 118
  • Thanks, this helped me understand how to create the JSON object... however, I am confused as to the first http request with android link... do I need to change the permission in the manifest file, because that link is the first time I saw a post say that (it is from 2010) also a lot is written vs what is mentioned here and in the link following it http://developer.android.com/reference/java/net/HttpURLConnection.html ... The following link how to send a JSON object over ... is also confusing me because of the timeout... is there a reason for the timeout? – Lion789 Jan 16 '14 at 00:22
  • @Lion789 I am not much of an expert on the permissions. I was just trying to give you some general guidelines. I guess HttpClient is a higher level API than HttpURLConnection but I am not fully sure. But so it seems to me looking at their methods. – peter.petrov Jan 16 '14 at 00:28
  • That is the problem with looking at posts, and I saw a bunch of different possible ways but each one confuses me... I was thinking it would be as simple as a standard way of doing an ajax get/post... – Lion789 Jan 16 '14 at 00:39
  • @Lion789 Well, I would just try all these approaches one by one starting from those which involve less third-party libraries. – peter.petrov Jan 16 '14 at 00:41
1

POST and GET, along with other REST calls require a little more code than that. There are several libraries to pick from. Retrofit is one of better documented ones. Broadly speaking, you need to setup a httpclient to make a call in a background task, which will then return a response (JSON) that you can parse. Here is a link to Retrofit.

soundsofpolaris
  • 566
  • 2
  • 12
  • I am thinking of using this because a library might just make this all easier, but I am on the fence because I rather not bring a library in that I don't need. – Lion789 Jan 16 '14 at 00:23
  • 1
    I understand your reservation, but from my experience you will need it, especially if you're app is going to get more complex than just one POST. There are plenty of "gotcha" problems with implementing background REST calls over HTTP. Granted, it's good to know what you're doing and not use 3rd party libraries as a crutch, but in this case you'll be turning to the experts. For example, here is a SO post on different methods for running the background task – soundsofpolaris Jan 16 '14 at 00:29
  • Ok makes sense... do you have any opinions on this framework for it https://jersey.java.net/ I found a good tutorial on it, that is why I ask... Also, is retrofit as simple as it looks on their site? Is this really all you need for a POST? @FormUrlEncoded @POST("/user/edit") User updateUser(@Field("first_name") String first, @Field("last_name") String last); – Lion789 Jan 16 '14 at 00:37
  • 1
    also, any library that gets it done for you at this point is a library that you probably need, considering you can't figure out how to do this from examples, tutorials, documentation, and google. http://stackoverflow.com/questions/6218143/android-post-json-using-http – Jon Jan 16 '14 at 00:59
  • 1
    @Lion789 I've never seen that library before, but one thing to consider is good Java != good Android. I'd stick with the open source, tested and peer reviewed Android-built library, i.e. Retrofit or something like it. – soundsofpolaris Jan 16 '14 at 01:06
0

The volley library mentioned at this year's Google io can handle a lot of the work for you. There is a guide I followed for an app I was writing at

http://java.dzone.com/articles/android-%E2%80%93-volley-library

chughes
  • 81
  • 5