看板 AndroidDev 關於我們 聯絡資訊
我使用 Volley 的 JsonObjectRequest 丟 JSONObject 給 PHP JSONObject 的資料是沒問題的 送之前有先印出來看 是{ "name":"aaaa" } 很簡單 到了 PHP 時 卻沒辦法解讀這筆資料 我看範例 好像 PHP應該是 $json_string = json_decode($_POST['name']); $json_associative_array = json_decode($json_string,true); echo json_encode($json_associative_array); 我試者把 我android 送給 php的資料 再送回給 android 我只是要確定 接收是否正確 不過結果都是 Error 好像都是 JSONObject 無法解析的問題 org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject 我PHP 只打echo json_encode(array('id' => '123', 'data' => 'abc')); android 是收的到的 所以表示傳輸功能 應該是正常的 就是 PHP接受json 在解析時有問題 我懷疑是 $_POST 的問題 $_POST 似乎需要 HTML 的功能配合 但我 json 的網址是打 "http://192.168.1.2/test/test01.php" 我是直接操作 php 並沒有透過 HTML 不知道該怎麼解決這個問題? -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 122.116.254.70 ※ 文章網址: http://www.ptt.cc/bbs/AndroidDev/M.1401568195.A.59A.html 我發現 $_POST 是空的..... 會是我送訊息的方式錯了嗎..... Map<String, String> map = new HashMap<String, String>(); map.put( "name", "aaaaa" ); JSONObject j = new JSONObject(map); JsonObjectRequest request = new JsonObjectRequest( url, j, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { main.Response(response); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.e("TAG", error.getMessage(), error); main.AppendTextViewMsg01("Error!" + error.getMessage() ); } }); mQueue.add(request); ※ 編輯: yhn0tgb60 (122.116.254.70), 06/01/2014 12:33:01 yhn0tgb60:轉錄至看板 PHP 06/01 12:40 解決了...... 不要用 $_POST 改成用 $data = json_decode(file_get_contents("php://input")); 我看到一堆範例用 $_POST..... 到底怎麼回事.... ※ 編輯: yhn0tgb60 (122.116.254.70), 06/01/2014 14:04:29
TsaoCCFGOGO:$_POST 是 php POST 時送出的資料 06/02 18:03
TsaoCCFGOGO:例:http://www.webtech.tw/info.php?tid=34 06/02 18:03