【发布时间】:2016-07-19 07:21:46
【问题描述】:
我正在搞乱并在 php/json 中创建一个 Web 服务。
<?php
$answer = array('status'=>401, 'answer'=>'');
if($numWeeks%2 == 0) {
$answer['answer'] = 'trash';
utf8_encode($answer['answer']);
$answer['status'] = 200;
}
else {
$answer['answer'] = 'trach/recycle';
utf8_encode($answer['answer']);
$answer['status'] = 200;
}
header('Content-type: application/json');
echo json_encode($answer);
?>
输出:
{"status":200,"answer":"trash"}
我也在尝试通过一个使用 RETROFIT (v1.9) 的 android 应用来阅读这篇文章。
我的对象类是:
public class TrashResponse {
public int status;
public String answer;
}
但我得到这个错误:
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
根据我的研究,它与我的网络服务中的 json 对象有关。我无法指出问题所在。
【问题讨论】: