php接收post json数据据怎么写

查看:25019|回复:2
助理工程师
我有这个JSON字符串,我想要POST这个数据到服务器,请问在Android上要怎么完成。
JSON字符串 {&clientId&:&ID:&,&device&:{&userAgent&:&myUA&,&capabilities&:{&sms&:true,&data&:true,&gps&:true,&keyValue&:{&Key2&:&MyValue2&,&Key1&:&myvalue1&}},&screen&:{&width&:45,&height&:32},&keyValue&:{&DevcKey2&:&myValue2&,&DevcKey1&:&myValue1&}},&time&:8}& &
复制代码我的问题是,如何把这个JSON数据POST到服务器上。
助理工程师
HttpPost request = new HttpPost(url);
// 先封装一个 JSON 对象
JSONObject param = new JSONObject();
param.put(&name&, &rarnu&);
param.put(&password&, &123456&);
// 绑定到请求 Entry
StringEntity se = new StringEntity(param.toString());
request.setEntity(se);
// 发送请求
HttpResponse httpResponse = new DefaultHttpClient().execute(request);
// 得到应答的字符串,这也是一个 JSON 格式保存的数据
String retSrc = EntityUtils.toString(httpResponse.getEntity());
// 生成 JSON 对象
JSONObject result = new JSONObject( retSrc);
String token = result.get(&token&);
助理工程师如何使用JSON格式 POST数据到服务器_百度知道function postSimpleData() {
type: "POST",
url: "/Service/SimpleData",
contentType: "application/json", //必须有
dataType: "json", //表示返回值类型,不必须
data: JSON.stringify({ 'foo': 'foovalue', 'bar': 'barvalue' }),
//相当于 //data: "{'str1':'foovalue', 'str2':'barvalue'}",
success: function (jsonResult) {
alert(jsonResult);
function postListString() {
type: "POST",
url: "/Service/ListString",
contentType: "application/json",
dataType: "json",
data: JSON.stringify({ "BuIds": ["1", "2", "3"] }),
success: function (jsonResult) {
alert(jsonResult);
function postEmployees() {
type: "POST",
url: "/Service/Employees",
contentType: "application/json",
dataType: "json",
data: JSON.stringify({
"Employees": [
{ "firstName": "Bill", "lastName": "Gates" },
{ "firstName": "George", "lastName": "Bush" },
{ "firstName": "Thomas", "lastName": "Carter" }
success: function (jsonResult) {
alert(jsonResult);
Controller
[HttpPost]
public ActionResult SimpleData(string foo, string bar)
return Json("SimpleData", JsonRequestBehavior.AllowGet);
[HttpPost]
public ActionResult ListString(List&string& buIds)
return Json("ListString", JsonRequestBehavior.AllowGet);
[HttpPost]
public ActionResult Employees(List&Employee& Employees)
return Json("Employees", JsonRequestBehavior.AllowGet);
public class Employee
public string FirstName { get; set; }
public string LastName { get; set; }
值得注意的有2点:
1)Ajax 选项中
contentType: "application/json"
&这一条必须写,表明request的数据类型是json。
dataType: "json"
这一条表示返回值的类型,不必须,且依据返回值类型而定。
data: JSON.stringify({ 'foo': 'foovalue', 'bar': 'barvalue' })
&很多时候我们将数据写作:
{ 'foo': 'foovalue', 'bar': 'barvalue' }
这样会导致错误,因为js会默认将这个json对象放到表单数据中,故而导致controller接收不到。
有两种办法处理:第一种方式是用JSON.stringify()函数,其中JSON被Ecmascript5定义为全局对象。有关该函数的用法,见。
& & & & & & & & & & 第二种方式是直接用双引号包裹起来,比如data: "{'str1':'foovalue', 'str2':'barvalue'}"。
阅读(...) 评论()jQuery使用 $.post提交json数据_悬赏任务_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
jQuery使用 $.post提交json数据
我需要一份与标题相关的文档
收到2篇文档
相似悬赏任务使用HttpClient通过post方式发送json数据 – 过往记忆
欢迎关注Hadoop、Hive、Hbase、Flume等微信公共账号:iteblog_hadoop。
文章总数:692
浏览总数:7,543,045
评论:4050
分类目录:80 个
注册用户数:1693
最后更新:日
欢迎关注微信公共帐号:iteblog_hadoop
  有时候我们在发送HTTP请求的时候会使用到POST方式,如果是传送普通的表单数据那将很方便,直接将参数到一个Key-value形式的Map中即可。但是如果我们需要传送的参数是Json格式的,会稍微有点麻烦,我们可以使用HttpClient类库提供的功能来实现这个需求。假设我们需要发送的数据是:
&blog&: &&,
&Author&: &iteblog&
我们可以通过JSONObject够着Json:
JSONObject jsonObject = new JSONObject();
jsonObject.put(&blog&, &&);
jsonObject.put(&Author&, &iteblog&);
如果需要使用Post方式来发送这个数据,我们可以如下实现:
private HttpMethodBase createMethod(String url, int timeout) {
PostMethod method =
method = new PostMethod(url);
JSONObject jsonObject = new JSONObject();
jsonObject.put(&blog&, &&);
jsonObject.put(&Author&, &iteblog&);
String transJson = jsonObject.toString();
RequestEntity se = new StringRequestEntity(transJson, &application/json&, &UTF-8&);
method.setRequestEntity(se);
//使用系统提供的默认的恢复策略
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
//设置超时的时间
method.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, timeout);
} catch (IllegalArgumentException e) {
logger.error(&非法的URL:{}&, url);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
  我们通过StringRequestEntity来构造请求实体,在这里,StringRequestEntity将接收三个参数,如下:
public StringRequestEntity(String content, String contentType, String charset)
  throws UnsupportedEncodingException
  其中参数content就是我们需要传输的数据;contentType是传送数据的格式,因为我们的数据格式是json的,所以contentType必须填写application/json(更多的contentType可以参见);charset是字符集编码。
  然后我们再通过HttpClient对象的executeMethod方法来执行:
int statusCode = httpClient.executeMethod(getMethod);
//只要在获取源码中,服务器返回的不是200代码,则统一认为抓取源码失败,返回null。
if (statusCode != HttpStatus.SC_OK) {
logger.error(&Method failed: & + getMethod.getStatusLine() + &\tstatusCode: & + statusCode);
pom.xml文件的关键内容
&dependencies&
&!--网络爬虫--&
&dependency&
&groupId&commons-httpclient&/groupId&
&artifactId&commons-httpclient&/artifactId&
&version&3.1&/version&
&/dependency&
&dependency&
&groupId&org.apache.httpcomponents&/groupId&
&artifactId&httpcore&/artifactId&
&version&4.3.1&/version&
&/dependency&
&dependency&
&groupId&com.google.guava&/groupId&
&artifactId&guava&/artifactId&
&version&14.0.1&/version&
&/dependency&
&dependency&
&groupId&org.json&/groupId&
&artifactId&json&/artifactId&
&version&&/version&
&/dependency&
&/dependencies&
完整代码下载
本博客文章除特别声明,全部都是原创!
禁止个人和公司转载本文、谢谢理解:
下面文章您可能感兴趣

我要回帖

更多关于 json数据接口怎么写 的文章

 

随机推荐