View error

Asked

Viewed 48 times

0

I have this query system in the msql database on the site, but I want to bring to the Oura Activity the login values and password. I tried with putExtra, but she doesn’t recognize the strings user_name and password. How do I get the values of user_name and password and send to another Activity?

follows code:

Mainactivity (only the action command)

button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                //pega os textos escritos
               String username = editText1.getText().toString();
               String password = editText2.getText().toString();
               String type = "login";
               BackgroundWorker backgroundWorker = new BackgroundWorker(MainActivity.this);
               backgroundWorker.execute(type, username, password);



            }
        });

Follow Backgroundworker.java Code

public class BackgroundWorker extends AsyncTask<String,Void,String> {

Context context;



AlertDialog alertDialog;

BackgroundWorker (Context ctx) {
    context = ctx;

}

@Override
protected String doInBackground(String... params) {
    String type = params[0];

    String login_url = "http://www.ic.eng.br/login.php";

    if(type.equals("login")) {
        try {

            String user_name = params[1];
            String password = params[2];

            URL url = new URL(login_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_name","UTF-8")+"="+URLEncoder.encode(user_name,"UTF-8")+"&"
                    + URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8");
            bufferedWriter.write(post_data);
            bufferedWriter.flush();
            bufferedWriter.close();
            outputStream.close();
            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
            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("Status do login");

}

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

    if(result.equals("Conectado")){

            // aqui o redirecionamento para a activity


                Intent i = new Intent(context, Principal.class);
                i.putExtra("login", user_name);
                i.putExtra("senha", password);

                context.startActivity(i);


            } else {
                // quando as credenciais estão incorretas
            }

}

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


}

1 answer

0

The easiest way is to put in Intent via putExtra and pick up the initiated Activity, I saw that you are doing this but do not have the code of the second Activity where you recover the values, can post it? Anyway the way to pass and take the value is like this:

Passing the value:

Intent intent = new Intent(getBaseContext(), SignoutActivity.class);
//EXTRA_SESSION_ID é a key que será usada para recuperar o valor que foi passada em sessionId
intent.putExtra("EXTRA_SESSION_ID", sessionId);
startActivity(intent);

Picking up the value on the other Activity:

String sessionId= getIntent().getStringExtra("EXTRA_SESSION_ID");

Withdrawn and adapted from: https://stackoverflow.com/a/2091482/6009128

  • It’s not working because I’m not able to carry String to onPostExecute. Since I couldn’t find a way, I’m using Sharedpreferences on Mainactivity and it worked. But thank you...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.