A Simple HTTTP Request client, that's requesting a string from a nodejs webserver.
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
Change http://10.0.2.2:3000/
to your IP-Address, unless your webserver is localhost.
Rememberlocalhost:3000 -> Android recognize localhost as: 10.0.2.2
Check/edit ID for button: Button contactServerButton = findViewById(R.id.button1);
add android:usesCleartextTraffic="true"
add <uses-permission android:name="android.permission.INTERNET" />
Check/edit ID button: android:id="@+id/button1"
File location: MainActivity.java: AndroidStudio-HTTPClient/app/src/main/java/com/example/clienttonodejs/MainActivity.java
File location: AndroidManifest.xml: AndroidStudio-HTTPClient/app/src/main/AndroidManifest.xml
File location: activity_main.xml: AndroidStudio-HTTPClient/app/src/main/res/layout/activity_main.xml
Code is adapted from:
Android AsyncTask HTTP GET request Tutorial and
Minimal clien-server example for Android and Node.js