Android开关(String)不起作用

时间:2018-03-19 19:18:41

标签: android if-statement switch-statement

我的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总是“Generic Error”。没有代码的错误。

1 个答案:

答案 0 :(得分:0)

问题出现在while循环

sb.append(line + "\n");

在这里,您要在字符串line中附加一个新行。取消\n或在\n中添加case。如下。

case "Success\n" case "Error\n"