Sending JSON body through POST request in OKhttp in Android -


i have setting okhttpclient , sending request server. , able sending post request server empty body tag.

now, i'm trying send following json object server.

{ "title": "mr.", "first_name":"nifras", "last_name": "", "email": "nfil@gmail.com", "contact_number": "75832366", "billing_address": "", "connected_via":"application" } 

for have trying adding okhttpclient library class requestbody fail sending json object body of http post request. following way have try build body , process post request.

okhttpclient client = new okhttpclient();      requestbody body = new requestbody() {         @override         public mediatype contenttype() {             return applicationcontants.json;         }          @override         public void writeto(bufferedsink sink) throws ioexception {               // place add json thought. how         }     };      request request = new request.builder()             .url(applicationcontants.base_url + applicationcontants.customer_url)             .post(body)             .build(); 

what way send json object server via post request.

thanks in advance.

try this

add gradle depends compile 'com.squareup.okhttp3:okhttp:3.2.0'

public static jsonobject foo(string url, jsonobject json) {         jsonobject jsonobjectresp = null;          try {              mediatype json = mediatype.parse("application/json; charset=utf-8");             okhttpclient client = new okhttpclient();              okhttp3.requestbody body = requestbody.create(json, json.tostring());             okhttp3.request request = new okhttp3.request.builder()                     .url(url)                     .post(body)                     .build();              okhttp3.response response = client.newcall(request).execute();              string networkresp = response.body().string();             if (!networkresp.isempty()) {                 jsonobjectresp = parsejsonstringtojsonobject(networkresp);             }         } catch (exception ex) {             string err = string.format("{\"result\":\"false\",\"error\":\"%s\"}", ex.getmessage());             jsonobjectresp = parsejsonstringtojsonobject(err);         }          return jsonobjectresp;     } 

parse response

   private static jsonobject parsejsonstringtojsonobject(final string strr) {      jsonobject response = null;     try {         response = new jsonobject(strr);     } catch (exception ex) {         //  log.e("could not parse malformed json: \"" + json + "\"");         try {             response = new jsonobject();             response.put("result", "failed");             response.put("data", strr);             response.put("error", ex.getmessage());         } catch (exception exx) {         }     }     return response; } 

Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -