The most popular way that clients communicate with servers is using the HTTP protocol. And since you want to send data to a server, a HTTP POST request is what you need.
There are two ways to do this, either using the HttpURLConnection class or using the HTTP library for Android called Volley. I’ll be using the latter here.
First, if you’re running Express - set up routing to handle the post requests that your server is going to recieve:
- var express = require('express');
- var app = express();
-
- const bodyParser = require('body-parser');
- var server = app.listen(3000);
-
- app.use(bodyParser.json());
- app.use(bodyParser.urlencoded({ extended: true }));
-
- app.post('/postdata', (req, res) => {
- var data = req.body.data; // your data
- // do something with that data (write to a DB, for instance)
- res.status(200).json({
- message: "Data received successfully"
- });
- });
Next, you need to write a Volley POST request in an Activity that sends data in JSON format, because that is the ideal way of communication with a RESTful Node server. In Android Studio it’s done like this:
- public class MainActivity extends AppCompatActivity {
- Button RequestButton; // button which on clicking, sends the request
- TextView DisplayText; // a text field to display the request response
- TextView DataField; // a text field where the data to be sent is entered
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- RequestButton = (Button) findViewById(R.id.RequestButton);
- DataField = (TextView) findViewById(R.id.DataField);
- DisplayText = (TextView) findViewById(R.id.DisplayText);
-
- final RequestQueue queue = Volley.newRequestQueue(this);
- final String url = "http://serverdomainorip/postdata"; // your URL
-
- queue.start();
- RequestButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- HashMap<String, String> params = new HashMap<String,String>();
- params.put("data", DataField.getText().toString()); // the entered data as the body.
-
- JsonObjectRequest jsObjRequest = new
- JsonObjectRequest(Request.Method.POST,
- url,
- new JSONObject(params),
- new Response.Listener<JSONObject>() {
- @Override
- public void onResponse(JSONObject response) {
- try {
- DisplayText.setText(response.getString("message"));
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- }, new Response.ErrorListener() {
- @Override
- public void onErrorResponse(VolleyError error) {
- DisplayText.setText("That didn't work!");
- }
- });
- queue.add(jsObjRequest);
- }
- });
- }
- }
Essentially, the above code takes the data entered in a text field and sends it as the body of a POST request to your node server on clicking a button on the page. Once the response is received, it sets that response as the text of another text field on the page for you to see it. Here, The data is sent as JSON by wrapping it in a JSON object and the response received is also in JSON format.