作者yhn0tgb60 (呦厚厚)
看板PHP
标题Fw: [问题] Volley的JsonObject PHP要怎麽接收?
时间Sun Jun 1 12:40:46 2014
※ [本文转录自 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://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
※ 发信站: 批踢踢实业坊(ptt.cc)
※ 转录者: yhn0tgb60 (122.116.254.70), 06/01/2014 12:40:46
1F:推 Darkland:第一个参数是 method 吧 应该要打 'post'? 06/01 12:55
2F:→ Darkland:Request.Method.POST 06/01 12:56
3F:→ Darkland:然後 php 那边 json_decode 择一就好了阿 06/01 12:57
解决了......
不要用 $_POST
改成用
$data = json_decode(file_get_contents("php://input"));
我看到一堆范例用 $_POST..... 到底怎麽回事....
※ 编辑: yhn0tgb60 (122.116.254.70), 06/01/2014 14:05:14
4F:推 Darkland:$_POST 的条件必须要让 request 有正确的 content-type 06/01 15:03
5F:→ Darkland:你这个编辑加上的这段是取 raw body 的方式. 06/01 15:03