Android String Post http

0

I have a code that I would like to do a "Post" of the email string after clicking the button, the code of the post apparently is correct, but the button is not working .. The logcat does not return any error..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();

                }
            }

        }
    
asked by anonymous 11.05.2017 / 23:04

2 answers

0

Instead of using the setOnClickListener method, try to create a "public void myClick ()" method in your activity and inserted into the xml of the layout on your Button android property: onClick="myClick".

This should work.

    
11.05.2017 / 23:49
0

Hello,

You are calling conn.connect () before creating the outputStream so the connection is made without the post parameters ....

try to put this excerpt after the output close and see if it works.

    
12.05.2017 / 00:02