Send parameters via post json android

Asked

Viewed 623 times

0

I need to send the username string via post after button1 is clicked, my code has no error in the monitor, simply does not send anything to the bank... This is my code :

public class PostTeste extends AppCompatActivity {


    private Button button1;
    private EditText username;
    String  mUsername;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home);

        button1 = (Button) findViewById(R.id.button1);
        username = (EditText) findViewById(R.id.etpost);

        button1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                String mUsername = username.getText().toString();
                // String  mPassword = password.getText().toString();

                //tryLogin(mUsername);

                new TryLogin().execute("Samara");
            }
        });
    }

    private class TryLogin extends AsyncTask<String, Void, String> {


        @Override
        protected String doInBackground(String... params) {

            final String mUsername = params[0];
            if (null == mUsername) {
                return "Username não informado";
            }

            HttpURLConnection connection;
            OutputStreamWriter request = null;

            URL url = null;
            String response = null;
            String parameters = "email=" + mUsername;

            try {
                url = new  URL("api_url");
                connection = (HttpURLConnection) url.openConnection();
                connection.setDoOutput(true);
                connection.setRequestProperty("Accept", "application/json");
                connection.setRequestProperty("X-DreamFactory-Api-Key", "XXXXXXXXXXXXXXXXXXXX");
                connection.setRequestProperty("X-DreamFactory-Session-Token", "key");
                connection.setRequestProperty("Authorization", "auth");
                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 from server after login process will be stored in response variable.
                response = sb.toString();
            /**
             Não podemos interar com a tela!, vamos mandar para o método onPostExecute
             */
            // Toast.makeText(this,"Message from Server:"+ response, 0).show();
                isr.close();
                reader.close();
                return response;

            } catch (IOException e) {
                e.printStackTrace();
                return e.getMessage();
            }

        }

    /**
     * Este método irá rodar na Thread de UI após executar a ação!
     *
     * @param s
     */

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            if (s != null) {
                Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
            }
        }
    }
}

1 answer

1

Ola, The.openConnection() url only creates the object, to actually make the connection add the following code after the request.close() : connection.connect();

  • Could you clarify more about where to put the code and what it does?

  • I edited in the post ;)

Browser other questions tagged

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