-1

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.

Krang
  • 53
  • 2
  • 10
  • 1
    Maybe output doesn't equal "EMPTY"? – rmlan Apr 20 '16 at 17:01
  • As @rmlan alludes to ... what is the value of `output`? – Al Lelopath Apr 20 '16 at 17:03
  • Thanks for responding. I don't understand the downvote, but that is o.k. output and result are the same. When I said that I tried many solutions, that is just what I did. I was hoping that perhaps some other eyes and experience could see what I was missing. – Krang Apr 20 '16 at 17:07
  • Check this answer - http://stackoverflow.com/a/1795436/3872500. Apply that technique to your result string and see what you get. – Rohan Apr 20 '16 at 17:31

1 Answers1

0

I'm going to consider this question that I opened, closed, unless someone else wants to chime in with some more information.

Thanks goes to @rmlan for reminding me about something I saw in one of my previous Log.e listing, because it comepletely slipped my mind. Now I feel like such a Newb.

@rmlan I'll uptick your reply, if I am allowed to.

"EMPTY", and any other results that I got all had a '?'/ question mark appended to the front of them. As far as I can tell, it did NOT come from the PHP side, as I trimmed the echo output before it was sent to android, so it leaves me to believe that it has something to do with the returning Unicode encoding.

After some more reasearch, I am assume that the question mark is what is called a Byte Order Mark (BOM).

Although the question mark would show up in the Log.e output, it wouldn't show up in the toast, or the temporary AlertDialog I made which contained an editText field.

The strange thing about all of this is that my conditional code was working fine, then all of a sudden it refused to work, which led me to find the question mark in the Log.e output.

A solution to my problem was to change this:

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.
}

to this:

if(output.contains("EMPTY")) {

Log.e("asd", output); // this should prove that the conditional worked.

finish();  // this is also here to prove that the conditional worked.
}

I don't want to use 'contains' but until I can find out where the question marks are coming from, I have to live with it.

If anyone knows where the question marks come from, please chime in.

I hope all of this helps some others.

Krang
  • 53
  • 2
  • 10