Put Json method to update data

Asked

Viewed 660 times

1

I have a screen that receives data from another Activity in your edittext , how to update this data and send to my database using the "put" method, I have a Post code, I will use it as an example ,I believe it is similar :

Example of "POST" :

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


    protected void onPreExecute() {
    }


    public String doInBackground(String... arg0) {


        try {


            URL url = new URL("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", "36fda24fe5588fa4285ac6c6c2fdfbdb6b6bc9834699774c9bf777f706d05a88");
            conn.setRequestProperty("X-DreamFactory-Session-Token", "xxxxxxxA");
            conn.setRequestProperty("Authorization", "Basic "xxxxx");
            conn.setRequestMethod("POST");
            conn.setReadTimeout(15000 /* milliseconds */);
            conn.setConnectTimeout(15000 /* milliseconds */);
            conn.setDoInput(true);
            conn.setDoOutput(true);



            postDataParams.put("campo do banco", string);
            postDataParams.put("tx_name", mName);
            postDataParams.put("tx_nickname", mNick);
            postDataParams.put("dt_nascimento", mData);
            postDataParams.put("tp_sexo", mSexo);
            postDataParams.put("nu_cellphone", mNumber);
            postDataParams.put("password", mPassword);
            postDataParams.put("tx_email", mEmail);
            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) {
                Intent intent = new Intent(MainActivity.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());
        }
    }
  • This line conn.setRequestProperty("Authorization", "Basic "xxxxx"); has a " the most after Basic

No answers

Browser other questions tagged

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