PUT method via JSON

Asked

Viewed 119 times

1

I have a user "update" screen that would update data in my database .. How to do this update in my bank in Android Studio ? My code is like this :

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_attcad);


        txtname1 = (EditText) findViewById(R.id.txtname1);
        txtnick1 = (EditText) findViewById(R.id.txtnick1);
        edittextpassword = (EditText) findViewById(R.id.edittextpassword);
        edittextnumcel = (EditText) findViewById(R.id.edittextnumcel);
        edittextID = (EditText) findViewById(R.id.tx_id);
        Tv_mail = (EditText) findViewById(R.id.tvmail);
        Attcadastro = (Button) findViewById(R.id.attcad);



            Attcadastro.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    nome = txtnick1.getText().toString();
                    nickname = txtnick1.getText().toString();
                    password = edittextpassword.getText().toString();
                    numcel = edittextnumcel.getText().toString();
                    email = Tv_mail.getText().toString();
                    id = edittextID.getText().toString();

                   new PatchClass().execute();


                }
            });
        }

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

            try {



                String urlLogin = "url";
                URL url = new URL(urlLogin);

                HttpURLConnection conn = (HttpURLConnection) url.openConnection();

                JSONObject postDataParams = new JSONObject();
                conn.setRequestProperty("Content-type", "application/json");
                conn.setRequestProperty("Accept", "application/json");
                conn.setRequestProperty("X-DreamFactory-Api-Key", "xxxxxxxxxx");
                conn.setRequestProperty("X-DreamFactory-Session-Token", "xxxxxxxxxx");
                conn.setRequestProperty("Authorization", "Basic dGhpYWdvLmNhbWFyZ29AZXZvbHV0aW9uaXQuY29tLmJyOmluaWNpYWwyMDE3'");
                conn.setDoOutput(true);
                conn.setRequestMethod("PUT");
                OutputStreamWriter out = new OutputStreamWriter(
                conn.getOutputStream());
                out.write("resource");

                conn.getInputStream();

                conn.connect();

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

                postDataParams.put("id_user",id);
                postDataParams.put("tx_name", nome);
                postDataParams.put("tx_nickname", nickname);
                postDataParams.put("password", password);
                postDataParams.put("nu_cellphone", numcel);
                postDataParams.put("tx_email", email);
                Log.e("resource", postDataParams.toString());


                System.out.println("After this Connection");

                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();

                System.out.println("After this writers");


                int responseCode = conn.getResponseCode();


                if (responseCode == HttpsURLConnection.HTTP_OK) {
                    System.out.println("Funcionou!!");
                    Intent intent = new Intent(ActivityAttCad.this, MainActivity2.class);
                    startActivity(intent);


                    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());
            }
        }
No answers

Browser other questions tagged

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