How to send an Object as JSON to a Server using NameValuePairs?

163 Views Asked by At

I am attempting to add some Book(title, author, year) into my book table in a server using an AsyncTask, but i am getting missing bookobject as JSONfrom the method addBook(from the server). So i made a bookObject like this:

JSONObject jObj = new JSONObject();
jObj.put("title", "JAVA");
jObj.put("author", "me");
jObj.put("year", 2005);

After that, i wanna use(my intention is to send this book Object):

List<NameValuePair> nameValuePairs = new ArrayList<>(1);
nameValuePairs.add(new BasicNameValuePair(jObj));        //Error
new AaaBookAsyncTask(this).execute(new Pair<>(nameValuePairs, httpClient));

The problem is BasicNameValuePaircannot applied to JSONObject, but now how can i send the book Object? - Any help or hints is very appreciated.Thanks, Carl

1

There are 1 best solutions below

1
Bidhan On

You could send the JSON as a string and then decode it on your server before storing it in your database.