JSON对象后到PHP脚本Android应用程序 [英] json object post to php script in android app
问题描述
我想这个Android $ C $下发送JSON对象到我的网站
I tried this android code for send json objects to my website
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 code
And this php code
<?php
$msg=$_POST["msg"];
$filename="androidmessages.html";
file_put_contents($filename,$msg."<br />",FILE_APPEND);
$androidmessages=file_get_contents($filename);
echo $androidmessages;
?>
这会告诉我{名:名} 但如果我用
It will show me {"name":"name "} but if i use
httppost.setHeader( "Content-Type", "application/json" );
它会显示nothing.I的遭遇有关JSON对象前,后都没有,但我认为出事wrong.I要发送一些用户信息到我的网站,并在网页上显示它可以请你告诉我,我需要改变为了克服这个问题 谢谢
it will show nothing.I have no before experince about json object post but i think something went wrong.I want to send some user information to my website and display it in web page can you please tell me what i need to change to overcome this problem Thank you
推荐答案
这取决于你想如何将数据传递给你的PHP脚本。
It depends how you want to pass the data to your PHP-script.
如果你想获得的JSON作为一个字符串在一个变量(或者其他人的补充),那么你不应该使用的内容类型应用/ JSON的。如果你只想发布JSON没有任何变量,您可以执行以下操作来代替:
If you want to get the JSON as a string in one variable (and maybe others in addition), then you should not use the content-type "application/json". If you want to only post the JSON without any variable, you can do the following instead:
HttpPost httppost = new HttpPost(url);
JSONObject j = new JSONObject();
j.put("name","name ");
httppost.setEntity(new StringEntity(j.toString());
httpclient.execute(httppost);
正如你可以从$ C $想象c您没有JSON在一个POST-VAR只,但总。
As you can imagine from the code you do not have the JSON in one POST-var only but in total.
这篇关于JSON对象后到PHP脚本Android应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!