我想构建一个小应用程序,其中应用程序将与JSON对象的帮助下与PHP脚本进行通信。我成功实现了GET请求测试应用程序,但是对帖子使用JSON会产生问题。该代码不会产生错误,但用什么都没有,除了一个空的Array(我的PHP脚本回复)这意味着什么用的代码连接发送:如何通过POST请求与Android发送JSON对象
<?php print_r($_REQUEST); ?>
与
<?php print($_REQUEST['json']); ?>
抛出HTML努力回到带有json变量未找到错误的应用程序。
我已经试过这里提到的几个解决方案,包括:How to send a JSON object over Request with Android?和How to send a json object over httpclient request with android所以如果你能指出我的错误并能简要描述我到底做错了什么,那将是非常好的。谢谢。
下面是JSON对象从哪里转换为字符串,然后附加到Post变量的代码片段。
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httppostreq = new HttpPost(wurl);
StringEntity se = new StringEntity(jsonobj.toString());
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httppostreq.setEntity(se);
//httppostreq.setHeader("Accept", "application/json");
//httppostreq.setHeader("Content-type", "application/json");
//httppostreq.setHeader("User-Agent", "android");
HttpResponse httpresponse = httpclient.execute(httppostreq);
HttpEntity resultentity = httpresponse.getEntity();
这里是Wireshark的,通过收集,如果它可以帮助TCP流转储:
POST /testmysql.php?test=true HTTP/1.1
Content-Length: 130
Content-Type: application/json;charset=UTF-8
Content-Type: application/json;charset=UTF-8
Host: 192.168.100.4
Connection: Keep-Alive
User-Agent: Apache-HttpClient/UNAVAILABLE (java 1.4)
{"weburl":"hashincludetechnology.com","header":{"devicemodel":"GT-I9100","deviceVersion":"2.3.6","language":"eng"},"key":"value"}HTTP/1.1 200 OK
Date: Mon, 30 Apr 2012 22:43:10 GMT
Server: Apache/2.2.17 (Win32)
Content-Length: 34
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html
Array
(
[test] => true
)
Test // echo statement left intentionally.
您是否尝试过使用数据包嗅探器来确定问题出在客户端还是服务器端? – Steve
@Steve脚本实际上是使用Array()发回数据包,并且如果我在URL的追加GET请求的地方,那么这些变量在请求数组中也会返回。只有这个JSON对象会造成问题。 我在我的i9100上通过局域网测试这个应用程序到我的安装了wamp(Apache服务器)的系统上,并且所有其他项目都正常工作 –