Can anyone tell me where I have messed up with my code?
I have tried many possible solutions to get this to work (including .toString().trim(); inside of onPostExecute).
I am using an HttpURLConnection to get responses from my server through a PHP script. My PHP script is "battle tested", and my Android code works fine until I try to get the response/result string value from the onPostExecute method into conditionals such as IF or Switch-Case, or a Log.
The result will work fine in a Toast, but not in Log.e or a conditional.
The asyncTask is inside of my MainActivity, but below/outside of the onCreate method.
This is the call to the AsyncTask:
new myAsyncTask().execute();
This is what is called in the MainActivity by onPostExecute:
// this method should receive the result from onPostExecute.
private void asyncTaskFinished(String output) {
Toast resultToast = Toast.makeText(getApplicationContext(), output + " at asyncTaskFinished", Toast.LENGTH_LONG);
resultToast.setGravity(Gravity.CENTER, 0, 0);
resultToast.show();
if(output.equals("EMPTY")) {
Log.e("asd", output); // this should prove that the conditional worked.
finish(); // this is also here to prove that the conditional worked.
}
}
This is the beginning of the AsyncTask:
private class myAsyncTask extends AsyncTask<String, Integer, String>{
@Override
protected String doInBackground(String... params) {
These are parts of the doInBackground method:
String response = stringBuilder.toString();
// Close response stream:
responseStream.close();
result = response.toString().trim();
return result;
This is the start of the onPostExecute method:
protected void onPostExecute(String result){
MainActivity.this.asyncTaskFinished(result); // this is what I am using to get the result into the MainActivity.
I don't believe that I have overlooked anything.
Thank you in advance.