如何检查结果

时间:2018-02-08 15:51:53

标签: java android

我有代码来注册女巫,当注册成功时可以返回1,而当注册成功时则可以返回2。 我希望在注册成功时开启新的活动。 但是,如果我做if(结果==" 1)它不起作用:/我在onPostExecute中添加它。 当注册成功时,我如何才能增加开放的新活动?

@Override
protected String doInBackground(String... params) {
    String type = params[0];
    String registery_url = "http://server/registery.php";
    if(type.equals("registery")){

        try {
            String user_login = params[1];
            String user_email = params[2];
            String user_password = params[3];
            String user_password2 = params[4];
            URL url = new URL(registery_url);
            HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            OutputStream outputStream = httpURLConnection.getOutputStream();
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
            String post_data = URLEncoder.encode("user_login", "UTF-8")+"="+URLEncoder.encode(user_login, "UTF-8")+"&"
                    +URLEncoder.encode("user_email", "UTF-8")+"="+URLEncoder.encode(user_email, "UTF-8")+"&"
                    +URLEncoder.encode("user_password", "UTF-8")+"="+URLEncoder.encode(user_password, "UTF-8")+"&"
                    +URLEncoder.encode("user_password2", "UTF-8")+"="+URLEncoder.encode(user_password2, "UTF-8");
            bufferedWriter.write(post_data);
            bufferedWriter.flush();
            bufferedWriter.close();
            outputStream.close();

            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
            String result = "";
            String line="";
            while((line=bufferedReader.readLine())!=null){
                result+=line;
            }
            bufferedReader.close();
            inputStream.close();
            httpURLConnection.disconnect();
            return result;
        }
        catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    return null;
}

@Override
protected void onPreExecute() {
    alertDialog = new AlertDialog.Builder(context).create();
    alertDialog.setTitle("Registery status");
}

@Override
protected void onPostExecute(String result) {
    alertDialog.setMessage(result);
    alertDialog.show();
}

@Override
protected void onProgressUpdate(Void... values) {
    super.onProgressUpdate(values);
}
}

1 个答案:

答案 0 :(得分:1)

问题是使用等于运算符==进行字符串比较。字符串是一个对象,如果在对象上使用==运算符,则检查引用相等性。相反,请"1".equals(result)。通过交换字符串和变量,可以避免使用NullPointerException