作者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://webptt.com/cn.aspx?n=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
1F:→ TsaoCCFGOGO:$_POST 是 php POST 时送出的资料 06/02 18:03