Fw: [問題] Volley的JsonObject PHP要怎麼接收?
※ [本文轉錄自 AndroidDev 看板 #1JYZl3MQ ]
作者: yhn0tgb60 (呦厚厚) 看板: AndroidDev
標題: [問題] Volley的JsonObject PHP要怎麼接收?
時間: Sun Jun 1 04:29:47 2014
我使用 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
※ 發信站: 批踢踢實業坊(ptt.cc)
※ 轉錄者: yhn0tgb60 (122.116.254.70), 06/01/2014 12:40:46
推
06/01 12:55, , 1F
06/01 12:55, 1F
→
06/01 12:56, , 2F
06/01 12:56, 2F
→
06/01 12:57, , 3F
06/01 12:57, 3F
解決了......
不要用 $_POST
改成用
$data = json_decode(file_get_contents("php://input"));
我看到一堆範例用 $_POST..... 到底怎麼回事....
※ 編輯: yhn0tgb60 (122.116.254.70), 06/01/2014 14:05:14
推
06/01 15:03, , 4F
06/01 15:03, 4F
→
06/01 15:03, , 5F
06/01 15:03, 5F
PHP 近期熱門文章
PTT數位生活區 即時熱門文章