Skip to content Skip to sidebar Skip to footer

Http Put Json Android

i try to develop an server/client app. Server is glassfish, client android(Google http api). I use rest to transport data. If i do a Get all is nice. Now i want to do a Put, but i

Solution 1:

I recommend you to use Volley (Also google API)

String url = "http://" + URL ;
    progressBar.setVisibility(View.VISIBLE); // in case you have progressBarStringRequest request = newStringRequest(Request.Method.PUT, url, newResponse.Listener<String>() {
        @OverridepublicvoidonResponse(String response) {
            progressBar.setVisibility(View.INVISIBLE);
            JSONObject jsonObject = null;
            user = null;
            try {
                jsonObject = newJSONObject(response);
                //do something usefull 

            } catch (JSONException e) {
                e.printStackTrace();
            }



        }
    }, newResponse.ErrorListener() {
        @OverridepublicvoidonErrorResponse(VolleyError error) {
            Log.e("volley error", error.toString());


        }
    }) {
        @OverridepublicStringgetBodyContentType() { //override //  the content type because the request is string not JSON //   request note that if you used JSON request it will don't work with restreturn"application/json";
        }
 //override getParams so that the server can receive the parameters otherwise //  parameters will be null at server  @OverrideprotectedMap<String, String> getParams() throws AuthFailureError {
            Map<String, String> parameters = newHashMap<String, String>();
            parameters.put("param1", myText.getText().toString());
            parameters.put("param2", myText.getText().toString());


            return parameters;
        }
    };

    requestQueue.add(request);

Solution 2:

You may use HttpURLConnection to get the job done.

URLurl=newURL("http://www.example.com/someAPI.php");

            HttpURLConnectionhttpURLConnection= (HttpURLConnection) url.openConnection();
            httpURLConnection.setDoOutput(true);
            // when you are PUT do make sure you assign appropriate header// In this case POST.

            httpURLConnection.setRequestMethod("PUT");
            httpURLConnection.connect();

            // like this you can create your JOSN object which you want to PUT what every JON body you like to PUTJsonObjectjsonObject=newJsonObject();
            jsonObject.addProperty("write", "whatever");
            jsonObject.addProperty("you", "want");

            // And this is how you will write to the URLDataOutputStreamwr=newDataOutputStream(httpURLConnection.getOutputStream());
            wr.writeBytes(jsonObject.toString());
            wr.flush();
            wr.close();

OkHttp

this provides more elegant way of GET, PUT, POST, DELETE

GET

    OkHttpClientokHttpClient=newOkHttpClient();
    Stringurl_String="API url";
    Requestrequest=newRequest.Builder()
            .url(url_String)
            .get()
            .build();
    okHttpClient.newCall(request).enqueue(newCallback() {
        @OverridepublicvoidonFailure(Call call, IOException e) {
            Log.d(TAG, "onFailure: ");
        }

        @OverridepublicvoidonResponse(Call call, Response response)throws IOException {
            Stringresponse_String= response.body().string();
            Log.d(TAG, "onResponse: \n"+response_String);
        }
    });

.

PUT
    OkHttpClientokHttpClient=newOkHttpClient();
    Stringurl_String="API url";
    MediaTypeJSON= MediaType.parse("application/json; charset=utf-8");
    RequestBodybody= RequestBody.create(JSON, "JSON body to be PUT");
    Requestrequest=newRequest.Builder()
            .url(url_String)
            .put(body)
            .build();
    okHttpClient.newCall(request).enqueue(newCallback() {
        @OverridepublicvoidonFailure(Call call, IOException e) {
            Log.d(TAG, "onFailure: ");
        }

        @OverridepublicvoidonResponse(Call call, Response response)throws IOException {
            Stringresponse_String= response.body().string();
            Log.d(TAG, "onResponse: \n"+response_String);
        }
    });

Post would work similarly like PUT, you would only require to replace put method by post.

Solution 3:

change @Query param to @form param

Post a Comment for "Http Put Json Android"