0

I have code to registery witch one can return me 1 when registery is succes and 2 when is not. And i want to open new activity when registery was succes. But if i make if (result == "1) it was not working :/ I add this in onPostExecute. How i can add open new activity when registery was succes?

@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);
}
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
F.Fipoo
  • 111
  • 1
  • 4
  • 12

1 Answers1

1

The problem is using the equality operator == for string comparison. A string is an object, and if you use the == operator on an object, you check for reference equality. Instead, do "1".equals(result). By swapping the string and the variable, you avoid a NullPointerException.

SeverityOne
  • 2,476
  • 12
  • 25
  • i do this like you talk. if("2".equals(result)) but its still dont work. But when i print my result,drop me 2. idk what i do wrong. – F.Fipoo Feb 08 '18 at 17:28
  • It's a bit difficult to say without the server handy. What happens if you do something like `System.out.printf("*%s*%n", result);` so that you can check for spaces? If that is the case, use `result.trim()` instead. – SeverityOne Feb 08 '18 at 17:52
  • I have whitespace and i was remove it now it work – F.Fipoo Feb 08 '18 at 17:59