Android开关(字符串)doesen';行不通
我的Android应用程序中有一个类,它向PHP页面发送HTTP POST请求。带有“成功”或“错误”的“回答”页面 我需要切换页面回答的内容,但切换案例和if-else链都不起作用。为什么?Android开关(字符串)doesen';行不通,android,if-statement,switch-statement,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;
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();
}
}
}
package com.example.mypackage;
导入android.content.Context;
导入android.os.AsyncTask;
导入android.widget.Toast;
导入java.io.BufferedReader;
导入java.io.IOException;
导入java.io.InputStreamReader;
导入java.io.OutputStreamWriter;
导入java.net.HttpURLConnection;
导入java.net.MalformedURLException;
导入java.net.URL;
公共类登录扩展了异步任务{
私有URL;
公共字符串用户名;
公共字符串密码;
私有HttpURLConnection;
私有OutputStreamWriter请求;
私有字符串响应;
私有字符串参数;
私有静态字符串结果;
私人语境;
受保护的布尔doInBackground(字符串…URL){
试一试{
parameters=“username=“+username+”&password=“+password;
url=新url(url[0]);
connection=(HttpURLConnection)url.openConnection();
connection.setDoOutput(真);
connection.setRequestProperty(“内容类型”、“应用程序/x-www-form-urlencoded”);
connection.setRequestMethod(“POST”);
请求=新的OutputStreamWriter(connection.getOutputStream());
请求。写入(参数);
request.flush();
request.close();
弦线;
InputStreamReader isr=新的InputStreamReader(connection.getInputStream());
BufferedReader读取器=新的BufferedReader(isr);
StringBuilder sb=新的StringBuilder();
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
response=sb.toString();
isr.close();
reader.close();
if(响应!=null){
//服务器说了些什么
返回true;
}否则{
//服务器什么也没说
返回false;
}
}捕获(格式错误){
e、 printStackTrace();
返回false;
}捕获(IOE异常){
e、 printStackTrace();
返回false;
}
}
受保护的void onPostExecute(布尔值成功){
if(success){//此变量是从“doInBackground”返回的
System.out.println(“从站点收到的消息:“+响应”);
发生了什么(反应);
System.out.println(“响应”的值:“+response”);
}否则{
System.err.println(“无需显示”);
}
}
//在MainActivity中调用此函数以设置参数
public void setCredentials(字符串用户名、字符串密码){
this.password=密码;
this.username=用户名;
}
//在MainActivity中调用此函数,从这里设置要使用Toast的上下文
public void setContext(LoginActivity LoginActivity){
this.context=逻辑活动;
}
私有void whathapped(字符串结果){
System.out.println(“结果”的值:“+result”);
开关(结果){
“成功”案例:
Toast.makeText(上下文,“登录成功”,Toast.LENGTH_SHORT).show();
打破
案例“错误”:
Toast.makeText(上下文,“登录失败,请重试”,Toast.LENGTH_LONG.show();
打破
违约:
Toast.makeText(上下文,“一般错误”,Toast.LENGTH_SHORT).show();
打破
}
如果(结果等于(“成功”)){
Toast.makeText(上下文,“登录成功”,Toast.LENGTH_SHORT).show();
}else if(result.equals(“Error”)){
Toast.makeText(上下文,“登录失败,请重试”,Toast.LENGTH_LONG.show();
}否则{
Toast.makeText(上下文,“一般错误”,Toast.LENGTH_SHORT).show();
}
}
}
当我测试bug时,系统日志中的输出对于所有步骤都是正确的(错误或成功),但显示的toast始终是“一般错误”。没有代码错误。问题在
while
循环中
sb.追加(行+“\n”)代码>
在这里,您将在字符串行
中添加一个新行。取下\n
或在案例中添加\n
。如下图所示
案例“成功\n”
案例“Error\n”
可能是result=“success”
,而不是“success”
?