安卓开关(字符串)不起作用



我的Android应用程序中有一个类,它向PHP页面发送HTTP POST请求。页面"回答"为"成功"或"错误"。

我需要切换页面回答的内容,但切换大小写和 if-else 链都不起作用。为什么?

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();
       }
    }
}

当我测试错误时,系统日志中的输出对于所有步骤都是正确的(错误或成功(,但显示的 toast 始终是"一般错误"。没有代码的错误。

问题出在while循环中

sb.append(line + "n");

在这里,您将向字符串 line 追加一个新行。取下该n或在case中添加n。如下。

case "Successn" case "Errorn"

最新更新