I have created my own server and I'd like to create Android application that would send request with data to it. I have already tested server with postman.
I tried to create POST request with use of this answer: https://stackoverflow.com/a/2938787/7143020 However unfortunately it seems that there may be no request send.
Related code is below:
@Override
protected String doInBackground(String... strings) {
String urlString = strings[0];
String data = strings[1];
OutputStream out = null;
OutputStream outputStream;
try {
URL url = new URL(urlString);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("POST");
urlConnection.setDoOutput(true);
outputStream = urlConnection.getOutputStream();
out = new BufferedOutputStream(urlConnection.getOutputStream());
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, "UTF-8"));
writer.write(data);
writer.close();
out.close();
urlConnection.connect();
} catch (Exception e) {
System.out.println(e.getMessage());
}
I tried with debugger and value for out was null so trouble may be related to
out = new BufferedOutputStream(urlConnection.getOutputStream());
In AndroidManifest.xml I have two permissions added:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
How can I resolve this issue?