アプリケーションがJSONオブジェクトの助けを借りてphpスクリプトと通信する小さなアプリケーションを構築しようとしています. GET Request テスト アプリケーションを正常に実装しましたが、投稿で JSON を使用すると問題が発生します。コードはエラーを生成しませんが、コードとの接続を介して何も送信されなかったことを意味する空の Array() を除いて、私の php スクリプトは何も返しません。
<?php print_r($_REQUEST); ?>
そして試してみました
<?php print($_REQUEST['json']); ?>
json variable not found エラーで HTML をアプリケーションに返します。
私はすでにここで言及されているいくつかの解決策を試しました: How to send a JSON object over Request with Android? Androidでhttpclientリクエストを介してjsonオブジェクトを送信する方法なので、私の間違いを指摘して、私が間違っていたことを簡単に説明していただければ幸いです。ありがとう。
以下は、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.