2013-08-16 65 views
1

我已经看到很多教程和问题,使用以下方法将JSON对象从Android发送到PHP。例如this wordpress blog,this codeproject tutorial和一些在stackoverflow的答案like these将POST JSON数据从Android POST到PHP的正确方法

所有这些教程和答案都使用HTTP标头来向这样的PHP发送数据(body)。

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

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

那么,有没有一种正确的方法可以将JSON数据从Android发送到PHP,而不需要在头文件中设置数据?

回答

6

如果使用NATIV LIB没有排球,这里是虚拟与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