Issue
I'm trying to read data from a webserver in the background using AsyncTask using the code below but it does not return the message i expect it to return.
private class MyAsyncTask extends AsyncTask<String, Void, String> {
String message;
@Override
protected String doInBackground(String... date) {
try {
URL checkIfBooked = new URL("http://10.0.2.2:80/folder/script.php");
try {
HttpURLConnection conn = (HttpURLConnection) checkIfBooked.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
String urlParameters = "date=" + date;
byte[] postData = urlParameters.getBytes(StandardCharsets.UTF_8);
OutputStream os = conn.getOutputStream();
os.write(postData);
os.flush();
os.close();
int responseCode = conn.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
BufferedReader in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
conn.disconnect();
message = response.toString();
} else {
// error
//TODO: let the user know that something is wrong with the network
}
} catch (java.io.IOException e) {
//TODO: prevent app from crashing
}
} catch (java.net.MalformedURLException e) {
//TODO: prevent app from crashing
}
return message;
}
@Override
protected void onPostExecute(String result) {
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
}
}
However if i do it using the following method I get the return message which is "The item has been booked..." The method:
void checkIfExists(String date){
try {
URL checkIfBooked = new URL("http://10.0.2.2:80/folder/script.php");
try {
HttpURLConnection conn = (HttpURLConnection) checkIfBooked.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches (false);
String urlParameters = "date=" + date;
byte[] postData = urlParameters.getBytes( StandardCharsets.UTF_8 );
OutputStream os = conn.getOutputStream();
os.write(postData);
os.flush();
os.close();
int responseCode = conn.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
BufferedReader in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
conn.disconnect();
Toast.makeText(this, response.toString(), Toast.LENGTH_LONG).show();
} else {
// error
//TODO: let the user know that something is wrong with the network
}
} catch (java.io.IOException e) {
//TODO: prevent app from crashing
}
} catch (java.net.MalformedURLException e){
//TODO: prevent app from crashing
}
}
I call MyAsyncTask using new MyAsyncTask().execute(DATE);
but it returns an empty string,
and I call checkIfExists
this way checkIfExists(DATE)
and it works.
Pls note that date is a final string "2018-03-14". Any help is welcome. Thanks.
Solution
This method doInBackground(String... date)
in AsyncTask
accepts String
varargs, yet you use it as if it is a single item.
This line String urlParameters = "date=" + date;
in your AsyncTask
should be : String urlParameters = "date=" + date[0];
- assuming only 1 String
passed.
I suspect you get an empty String
as this line if(responseCode == HttpURLConnection.HTTP_OK) ...
is false (if script.php
actually does some parsing?) and returns the uninitialised instance variable.
Answered By - Mark
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.