I created a REST API which appears to work fine (I tested the GET request of interest using Postman).
I am working to make an identical request from an Android application using an AsyncTask. The hope is to assign a string value generated by the AsyncTask to a string variable in the Android activity.
I have used a Toast to view the string output of the doInBackground method, which is passed to the onPostExecute method of the AsyncTask, to make sure that the call to the API is working and it works fine.
The problem comes when I assign the string output of the AsyncTask to the string variable defined in the Activity class. After executing the AsyncTask I am using a Toast to view the value of the string variable and it is reflecting the assignment performed in the onPostExecute method.
I will now share the code that I am using in the hope that you can help me to find where I might be going wrong. After the button is clicked, I intend for the validate AsyncTask to execute and assign a value to emailValid.
In view of the code below, my question in the most direct way I can put it: "Why is Toast 2 displaying while Toast 1 is not?"
Thank you in advance for your assistance.
public class JoinActivity extends AppCompatActivity implements View.OnClickListener
{
EditText email_et;
Button join_b;
String emailValid;
@Override
protected void onCreate(Bundle savedInstance)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_join);
emailValid = "No";
join_b = (Button) findViewById(R.id.aj_join_b);
join_b.setOnClickListener(this);
}
@Override
public void onClick(View view)
{
email_et = (EditText) findViewById(R.id.et_aj_email_address);
new JoinActivity.validate().execute(email_et.getText().toString());
// Toast 1 here:
Toast.makeText(getApplicationContext(), emailValid,
Toast.LENGTH_LONG).show();
}
private class validate extends AsyncTask<String, Void, String>
{
@Override
protected String doInBackground(String... params)
{
// ... GET request produces string with either yes or no i.e. outcome
return outcome;
}
@Override
protected void onPostExecute (String s)
{
// Toast 2 here:
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
emailValid = s;
super.onPostExecute(s);
}
}
}
As requested, the detail in the doInBackground method is pasted below. The method actually return some XML. My original question which said "a string variable which is either a "yes" or "no"" was my attempt to simplify the setup. As I mentioned before, displaying the string in a Toast from within the onPostExecute works fine showing the participantXML sent from the doInBackground:
@Override
protected String doInBackground(String... params) {
InputStream inputStream;
String emailAddress = params[0];
String outcome = null;
HttpURLConnection httpURLConnection = null;
BufferedReader bufferedReader = null;
try
{
URL url = new URL("http://10.0.2.2:8080/project_name/webresources/entity.participant/email");
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setRequestProperty("Accept", "application/xml");
httpURLConnection.addRequestProperty("Email-address", emailAddress);
httpURLConnection.connect();
inputStream = httpURLConnection.getInputStream();
StringBuffer stringBuffer = new StringBuffer();
if (inputStream == null) {
return null;
}
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuffer.append(line + "\n");
}
if (stringBuffer.length() == 0) {
return participantXML;
}
participantXML = stringBuffer.toString();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (httpURLConnection != null) {
httpURLConnection.disconnect();
}
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
validEmailAddress = participantXML;
return participantXML;
}