正确的方法来从Android的POST JSON数据到PHP方法来、正确、数据、POST

2023-09-05 10:13:09 作者:收养单身美男

我看过很多教程和问题,用下面的方法从机器人发送JSON对象到PHP。例如这个词preSS博客,的这$ C $的CProject教程和计算器的这样的。

I have seen many tutorials and questions using the following method to send JSON object to PHP from Android. For example this wordpress blog, this codeproject tutorial and some answers on stackoverflow like these.

所有这些教程和答案使用 HTTP头发送数据(体)到PHP这样的。

All of these tutorials and answers use HTTP header to send data(body) to PHP like this.

....
// Post the data:
httppost.setHeader("json",json.toString());
....

作为一个程序员,我们都知道,标题并不意味着进行数据(体)。头应该只进行元数据。

As a programmer we all know that headers are not meant to carry data(body). Headers should only carry metadata.

那么,有没有为JSON数据的Andr​​oid发送到PHP不涉及在头设置数据的正确方法是什么?

So, is there a correct way to send JSON data to PHP from Android which does not involve setting data in header?

推荐答案

如果您使用NATIV LIB没有排球,这里是虚拟与HttpClient的:

If you use nativ lib without Volley, here is dummy with HttpClient:

httpClient = createHttpClient();

//You wanna use POST method.
mPost = new HttpPost(_urlStr);

//Head
mPost.addHeader(new BasicHeader("Content-Type", "application/json"));

//Body
((HttpPost) mPost).setEntity(new StringEntity(jsonText));

//Do it.
client.execute(mPost);

尽量使用排:https://github.com/ogrebgr/android_volley_examples/blob/master/src/com/github/volley_examples/Act_SimpleRequest.java