json对象发布到android应用中的php脚本

json对象发布到android应用中的php脚本,第1张

概述我尝试了这个android代码以将json对象发送到我的网站HttpPosthttppost=newHttpPost(url);JSONObjectj=newJSONObject();j.put("name","name");List<NameValuePair>nameValuePairs=newArrayList<NameValuePair>(2);Stri

我尝试了这个android代码以将Json对象发送到我的网站

httpPost httppost = new httpPost(url);JsONObject j = new JsONObject();    j.put("name","name ");        List<nameValuePair> nameValuePairs = new ArrayList<nameValuePair>(2);        String format = s.format(new Date());     nameValuePairs.add(new BasicnameValuePair("msg",j.toString() ));       httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));                               httpclIEnt.execute(httppost);

而这个PHP代码

<?PHP$msg=$_POST["msg"]; $filename="androIDmessages.HTML";file_put_contents($filename,$msg."<br />",file_APPEND);$androIDmessages=file_get_contents($filename);echo $androIDmessages;?>

它将向我显示{“ name”:“ name”}
但是如果我用

httppost.setheader( "Content-Type", "application/Json" );

它不会显示任何内容.我以前没有关于Json对象发布的经验,但是我认为出了点问题.我想向我的网站发送一些用户信息,并在网页中显示它,请告诉我我需要更改以克服此问题问题
谢谢

解决方法:

这取决于您如何将数据传递到PHP脚本.

如果要在一个变量(或其他变量)中以字符串形式获取JsON,则不应使用内容类型“ application / Json”.如果您只想发布不带任何变量的JsON,则可以执行以下 *** 作:

httpPost httppost = new httpPost(url);JsONObject j = new JsONObject();j.put("name","name ");httppost.setEntity(new StringEntity(j.toString());httpclIEnt.execute(httppost);

正如您从代码中可以想象的那样,您仅在一个POST-var中没有JsON,而是总共有一个.

总结

以上是内存溢出为你收集整理的json对象发布到android应用中的php脚本全部内容,希望文章能够帮你解决json对象发布到android应用中的php脚本所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1076537.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存