I have a class in my Android app which send an HTTP POST request to a PHP page. The page "answer" with "Success" or "Error".
I need to switch what the page answered but both switch case and if-else chain doesen't work. Why?
package com.example.mypackage;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class LoginAction extends AsyncTask<String, Void, Boolean> {
private URL url;
public String username;
public String password;
private HttpURLConnection connection;
private OutputStreamWriter request;
private String response;
private String parameters;
private static String result;
private Context context;
protected Boolean doInBackground(String ... urls){
try {
parameters = "username="+username+"&password="+password;
url = new URL(urls[0]);
connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestMethod("POST");
request = new OutputStreamWriter(connection.getOutputStream());
request.write(parameters);
request.flush();
request.close();
String line;
InputStreamReader isr = new InputStreamReader(connection.getInputStream());
BufferedReader reader = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null){
sb.append(line + "\n");
}
response = sb.toString();
isr.close();
reader.close();
if (response != null){
//The server said something
return true;
} else {
//The server didn't said nothing
return false;
}
} catch (MalformedURLException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
protected void onPostExecute(Boolean success){
if (success){ //this variable is the return from "doInBackground"
System.out.println("Message recived from site: "+response);
whatHappened(response);
System.out.println("Value of \"response\": "+response);
} else {
System.err.println("Nothing to show");
}
}
//Call this in MainActivity to set the parameters
public void setCredentials(String username, String password){
this.password = password;
this.username = username;
}
//Call this in MainActivity to set the Context to use Toast from here
public void setContext(LoginActivity loginActivity){
this.context = loginActivity;
}
private void whatHappened(String result){
System.out.println("Value of \"result\": "+result);
switch (result){
case "Success":
Toast.makeText(context, "Login success", Toast.LENGTH_SHORT).show();
break;
case "Error":
Toast.makeText(context, "Login failed, please re-try", Toast.LENGTH_LONG).show();
break;
default:
Toast.makeText(context, "Generic error", Toast.LENGTH_SHORT).show();
break;
}
if (result.equals("Success")){
Toast.makeText(context, "Login success", Toast.LENGTH_SHORT).show();
} else if (result.equals("Error")){
Toast.makeText(context, "Login failed, please re-try", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context, "Generic error", Toast.LENGTH_SHORT).show();
}
}
}
When I test the bug the output in the System log is correct (Error or Success) for all the steps, but the toast shown is always "Generic Error". No code's errors.
Aucun commentaire:
Enregistrer un commentaire