Android String Post http

Asked

Viewed 65 times

1

I have a code that I would like you to do a "Post" of the email string after clicking on the button, the post code is apparently correct , but the button is not working.. logcat does not return any errors.. this is my code :

public class PostTeste extends AppCompatActivity {


    private Button button1;
    private EditText username;
    public String email;

    @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 email = username.getText().toString();
              //  String password = editTextPassword.getText().toString();

                //System.out.println(email);
                //System.out.println(password);
                //String cep = mCep.getText().toString();
                //Toast.makeText(LoginActivity.this,"Botão funcionou!!!",Toast.LENGTH_LONG).show();
            }
        });
    }

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

                protected void onPreExecute() {
                }

                protected String doInBackground(String... arg0) {

                    try {

                        URL url = new URL("Api URL);

                        JSONObject postDataParams = new JSONObject();




                        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                        conn.setRequestProperty("Content-type", "application/json");
                        conn.setRequestProperty("Accept", "application/json");
                        conn.setRequestProperty("X-DreamFactory-Api-Key", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
                        conn.setRequestProperty("X-DreamFactory-Session-Token", "xxxxxxxxx.xxxxxxxxxxxxxxxxxxxxxxxx.xxxxxxxxxxxxxxxxx");
                        conn.setRequestProperty("Authorization", "Basic dGhpYWdvLmNhbWFyZ29AZXZvbHV0aW9uaXQuY29tLmJyOmluaWNpYWwyMDE3");
                        //conn.setRequestProperty("-d", "{}");
                        conn.setRequestMethod("POST");
                        conn.setReadTimeout(15000 /* milliseconds */);
                        conn.setConnectTimeout(15000 /* milliseconds */);
                        conn.setDoInput(true);
                        conn.setDoOutput(true);

                        postDataParams.put("email", "email");
                        //postDataParams.put("password", "password");
                        Log.e("resource", postDataParams.toString());

                        JSONObject resource = new JSONObject();
                        JSONArray array = new JSONArray();
                        array.put(postDataParams);
                        resource.put("resource", array);

                        System.out.println(resource.toString());


                        conn.connect();

                        OutputStream os = conn.getOutputStream();
                        BufferedWriter writer = new BufferedWriter(
                                new OutputStreamWriter(os, "UTF-8"));
                        //writer.write(getPostDataString(postDataParams));
                        writer.write(resource.toString());

                        writer.flush();
                        writer.close();
                        os.close();

                        int responseCode = conn.getResponseCode();

                        if (responseCode == HttpsURLConnection.HTTP_OK) {

                            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                            StringBuffer sb = new StringBuffer("");
                            String line = "";

                            while ((line = in.readLine()) != null) {

                                sb.append(line);
                                break;
                            }

                            in.close();
                            return sb.toString();
                        } else {
                            return new String("false : " + responseCode);
                        }
                    } catch (Exception e) {
                        return new String("Exception: " + e.getMessage());
                    }
                }

                @Override
                protected void onPostExecute(String result) {
                    Toast.makeText(getApplicationContext(), result,
                            Toast.LENGTH_LONG).show();

                }
            }

        }

2 answers

0

Instead of using the setOnClickListener method, try creating a "public void meuClick()" method in your Activity and inserted in the layout xml on your Button the property android:onClick="meuClick".

That should work.

0

Hello,

Voce is calling Conn.connect() before creating outputStream so the connection is done without post parameters....

tries to put this snippet after close of output and see if it is right.

Browser other questions tagged

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