Send parameters via post json android

0
  

I need to send the string username via post after the button1 is clicked, my code has no error in the monitor, it 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();
            }
        }
    }
}
    
asked by anonymous 11.05.2017 / 21:50

1 answer

0

Hello, The url.openConnection (); just create the object, to actually make the connection add the following code after the request.close (): connection.connect();

    
11.05.2017 / 22:05