Search code examples
androidjsonrestandroidhttpclient

Send "PUT" request in Android to rest api


I have an android application that consulting and insert in a web service rest-ful. all this through apache HTTPClient and JSON.

so for example I insert a new user into db.

HttpClient httpclient = new DefaultHttpClient();
        // 2. make POST request to the given URL
            HttpPost httpPost = new HttpPost(url);
            String json = "";
            // 3. build jsonObject
            JSONObject jsonObject2 = new JSONObject();
            jsonObject2.put("name", name);
            jsonObject2.put("number", num);
         // 4. convert JSONObject to JSON to String
            json = jsonObject.toString();
        // 5. set json to StringEntity
            StringEntity se = new StringEntity(json);
        // 6. set httpPost Entity
            httpPost.setEntity(se);
        // 7. Set some headers to inform server about the type of the content   
            httpPost.setHeader("Accept", "application/json");
            httpPost.setHeader("Content-type", "application/json");
            // 8. Execute POST request to the given URL
            HttpResponse httpResponse = httpclient.execute(httpPost);

all perfectly created, well now I want to use the method that I created in the rest api, method PUT to overwrite for example the name of the user with ID 5 If I want to do a get enter my url + / ID and get a particular user. to "PUT" I do this but does not work.

 @Override
    protected String doInBackground(String... params) {
        InputStream inputStream = null;
        String result = ""; 

        try {
            // 1. create HttpClient
            HttpClient httpclient = new DefaultHttpClient();
            // 2. make POST request to the given URL

            HttpPut httpPut = new    
            HttpPut("http://000.000.0.000:0000/xxxxxx/webresources/net.xxxxx.users/5");
            String json = "";
            //              // 3. build jsonObject
            //              JSONObject jsonObject2 = new JSONObject();
            //              jsonObject2.put("idGuarderias", idG);
            // 3. build jsonObject
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("name",newName);
            //      jsonObject.put("guarderiasIdGuarderias",jsonObject2);
            json = jsonObject.toString();
            StringEntity se = new StringEntity(json);
            // 6. set httpPost Entity
            httpPut.setEntity(se);
            // 7. Set some headers to inform server about the type of the content   
            httpPut.addHeader("Accept", "application/json");
            httpPut.addHeader("Content-type", "application/json");
            // 8. Execute POST request to the given URL
            HttpResponse httpResponse = httpclient.execute(httpPut);

        } catch (Exception e) {
            Log.d("InputStream", e.getLocalizedMessage());
        }

What changes should I make?


Solution

  • I solved my problem with the following code, thanks for replying.

    @Override
        protected String doInBackground(String... params) {
            InputStream inputStream = null;
            String result = ""; 
    
            try {
                // 1. create HttpClient
                HttpClient httpclient = new DefaultHttpClient();
                // 2. make POST request to the given URL
                HttpPut httpPUT = new    
                       HttpPut("http://xxx.xx.x.xxx:xxxx/xxxxxxxy/webresources/net.xxxxxx.users/3");
                String json = "";
                // 3. build jsonObject
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("idUser","3");
                jsonObject.put("name","Mark");
                jsonObject.put("pass","1234");
                jsonObject.put("rol","554");
                jsonObject.put("usuario","mark");
    
    
    
    
                // 4. convert JSONObject to JSON to String
                json = jsonObject.toString();
    
                // 5. set json to StringEntity
                StringEntity se = new StringEntity(json);
                // 6. set httpPost Entity
                httpPUT.setEntity(se);
                // 7. Set some headers to inform server about the type of the content   
                httpPUT.setHeader("Accept", "application/json");
                httpPUT.setHeader("Content-type", "application/json");
                // 8. Execute POST request to the given URL
                HttpResponse httpResponse = httpclient.execute(httpPUT);
                // 9. receive response as inputStream
                //                  inputStream = httpResponse.getEntity().getContent();
                //                  // 10. convert inputstream to string
                //                  if(inputStream != null)
                //                      result = convertInputStreamToString(inputStream);
                //                  else
                //                      result = "Did not work!";
            } catch (Exception e) {
                Log.d("InputStream", e.getLocalizedMessage());
            }
    
            return "EXITO!";
        }
    

    They should put the id in the url, and also overwrite all parameters including the id