I can't figure out what is up with this code. First of all, as it is here, it works. But I'm afraid I have missed an important concept and I don't want it to come back and bite me later. My code below will write a blank line to a text log that I have created for testing purposes if I don't add the line "connection.getResponseMessage.' It will also work with 'getResponseCode.' Why?
Why does it write an empty buffer to the OutputStream without these codes?
public class AdminActivity extends AppCompatActivity {
Context mContext;
private static final String TAG = "AdminActivity";
EditText mSystemID, mSystemPassword;
RecyclerView mRecyclerView;
Button mUpdateButton, mDownloadButton;
FileItemAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin);
mContext = this;
mSystemID = findViewById(R.id.et_system_id);
mSystemPassword = findViewById(R.id.et_system_password);
mRecyclerView = findViewById(R.id.rv_file_names);
mUpdateButton = findViewById(R.id.bt_update_files_list);
mDownloadButton = findViewById(R.id.bt_download_file);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(layoutManager);
DividerItemDecoration divider = new DividerItemDecoration(mContext, layoutManager.getOrientation());
divider.setDrawable(ContextCompat.getDrawable(mContext, R.drawable.divider_dark));
// TESTING TESTING TESTING TESTING TESTING //
mUpdateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new SendInfoToServer().execute("A new Test");
}
});
}
private static class SendInfoToServer extends AsyncTask<String, String, String> {
HttpURLConnection connection = null; //***** Should eventually change to https instead of http
OutputStream out = null;
@Override
protected String doInBackground(String... params) {
String parameters = params[0];
try {
URL url = new URL("http://www.example.com/login/webhook.php");
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.connect();
out = new DataOutputStream(connection.getOutputStream());
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, "UTF-8"));
writer.write(parameters);
writer.flush();
writer.close();
Log.d(TAG, "response message: " + connection.getResponseMessage());
} catch (IOException e) {
Log.d(TAG, "an error occured");
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
}
return null;
}
}
}
I've tried to ask this question multiple different ways and nobody has given me any useful input. This, I guess, is my last attempt to simplify the question. Above is the entire Activity. It has already been suggested to remove 'writer.close()' but that didn't work.
Thanks in advance