如何使用NameValuePairs将对象作为JSON发送到服务器?

时间:2022-12-01 16:08:05

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:

我试图在使用AsyncTask的服务器中将一些Book(标题,作者,年份)添加到我的book表中,但是我从方法addBook(来自服务器)中获取了作为JSON的bookobject。所以我做了一个像这样的bookObject:

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

问题是BasicNameValuePair无法应用于JSONObject,但现在我如何发送书籍对象? - 非常感谢任何帮助或提示。谢谢,卡尔

1 个解决方案

#1


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

您可以将JSON作为字符串发送,然后在将其存储在数据库中之前在服务器上对其进行解码。

#1


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

您可以将JSON作为字符串发送,然后在将其存储在数据库中之前在服务器上对其进行解码。