PUT or database update

Asked

Viewed 44 times

1

How to update objects in my database via put json? This is my post code as an example,

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


        protected void onPreExecute() {
        }


        public String doInBackground(String... arg0) {


            try {


                URL url = new URL("http://uri.com/api/v2/bookdemo/_table/cad_users");

                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", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.XXXXXXXXXXXX.jfGbU3yjIPPfAQWTyYVQJCfEapYFnHPCYkL4T-arD4M");
                conn.setRequestProperty("Authorization", "Basic XXXXXXXXXXXXXXX");
                conn.setRequestMethod("post");
                conn.setReadTimeout(15000 /* milliseconds */);
                conn.setConnectTimeout(15000 /* milliseconds */);
                conn.setDoInput(true);
                conn.setDoOutput(true);


                postDataParams.put("tx_name", nome);
                postDataParams.put("tx_nickname", nickname );
                postDataParams.put("nu_cellphone", numcel );
                postDataParams.put("password", password );
                postDataParams.put("tx_email", email );
                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();

Making it clear that this is my post code used only for example , I would like an example of put code to update my database.

1 answer

0

public HttpClient client;
public Uri usuarioUri;

client = new HttpClient();
                client.BaseAddress = new Uri("URL");

var x = JsonConvert.SerializeObject(p);

//HttpContent newp = stringContent;

StringContent queryString = new StringContent(x);

System.Net.Http.HttpResponseMessage response;
response = await client.PostAsync("URL", queryString);
if (response.IsSuccessStatusCode)
{
    // Get the URI of the created resource.
    Uri gizmoUrl = response.Headers.Location;
}
  • Could be clearer?

  • Sorry, this code is for post. but I did a test with the code I found in this post take a look: https://stackoverflow.com/questions/15910677/how-to-call-put-method-from-web-apig-httpclient

  • Can be used for android?

  • They are components C#, if you are developing in Xamarin yes.

Browser other questions tagged

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