PHP インターフェイスを介してオンラインの mysql データベースからデータを取得できるシンプルなアプリを構築しようとしています。ただし、JSON を取得し、gson を使用してローカル Java オブジェクトに解析しようとすると、以下のエラーが発生し続けます。これはPHPへの私の最初の進出であるため、配列の作成に使用されるPHPメソッドに関連していると思います。誰かが見てくれれば助かります。
実行時のエラー:
10-10 17:51:30.146: W/System.err(683): Caused by: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 1
「ジョブ」の配列を返す PHP メソッド:
public function getAllJobs($email) {
$result = mysql_query("SELECT * from from job WHERE email = '$email'") or die(mysql_error());
$rows = array();
while (($r = mysql_fetch_array($result)) {
$rows[$r['jobId'][$r['desc']] = array(
'techId' => $r['techId'],
'email' => $r['email'],
'dateCreated' => $r['dateCreated'].
'dateClosed' => $r['dateClosed']
);
}
return $rows;
}
データベース関数クラスへの呼び出し:
} else if ($tag == 'GetAll') {
$email = $_POST['email'];
$password = $_POST['password'];
$result = array();
$result = $db->getAllJobs($email);
echo json_encode($result);
}
php/sql クエリから返されるジョブ クラス:
public class Job extends Message{
public Job(MessageType m)
{
this.messageType = m.toString();
}
public String messageType ;
public String jobId;
public String email;
public String techId;
public String desc;
public Date dateCreated;
public Date dateClosed;
@Override
public String getType() {
return messageType;
}
}
そして最後に、クラス CloudConnect の Java コード:
public synchronized List<Message> getAll(Message m) throws IOException {
List<Message> mList = new ArrayList<Message>();
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(site);
post.setEntity(new UrlEncodedFormEntity(validateMessage(m)));
HttpResponse response = client.execute(post);
StatusLine status = response.getStatusLine();
if ( status.getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
is = entity.getContent();
try {
Reader read = new InputStreamReader(is);
mList = Arrays.asList(gson.fromJson(read, Message[].class));
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return mList;
}