【问题标题】:Android switch (String) doesen't workAndroid 开关(字符串)不起作用
【发布时间】:2018-03-19 19:18:40
【问题描述】:

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

我需要切换页面回答的内容,但 switch case 和 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 始终是“通用错误”。没有代码错误。

【问题讨论】:

  • 可能是result = "success",而不是"Success"

标签: android if-statement switch-statement


【解决方案1】:

问题出现在while 循环中

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-18
    • 2011-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-04
    • 2017-07-25
    相关资源
    最近更新 更多