Send "PUT" request in Android to rest api

Emmanuel Guther picture Emmanuel Guther · Sep 11, 2014 · Viewed 23.4k times · Source

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?

Answer

yhel picture yhel · Sep 11, 2014

Maybe you can try this:

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


        //Try to add this
       inputStream = httpResponse.getEntity().getContent();

        if(inputStream != null)
            result = convertInputStreamToString(inputStream);
        else
            result = "Did not work!";

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