正确的方法来从Android的POST JSON数据到PHP [英] Correct way to POST JSON data from Android to PHP

查看:137
本文介绍了正确的方法来从Android的POST JSON数据到PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过很多教程和问题,用下面的方法从机器人发送JSON对象到PHP。例如<一个href="http://fahmirahman.word$p$pss.com/2011/04/26/the-simplest-way-to-post-parameters-between-android-and-php/"相对=nofollow>这个词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);

尽量使用排:<一href="https://github.com/ogrebgr/android_volley_examples/blob/master/src/com/github/volley_examples/Act_SimpleRequest.java">https://github.com/ogrebgr/android_volley_examples/blob/master/src/com/github/volley_examples/Act_SimpleRequest.java

这篇关于正确的方法来从Android的POST JSON数据到PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆