I'm trying to build a simple app that is able to retrieve data from an online mysql database through a PHP interface. However I keep receiving the error below when trying to retrieve the JSON and parse using gson into a local Java object. I believe it is related to the PHP methods used to create the array since this is my first foray into PHP so it would be helpful if anyone could take a look.
error during runtime:
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 method to return an array of 'Jobs':
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;
}
Calls to the Database function class:
} else if ($tag == 'GetAll') {
$email = $_POST['email'];
$password = $_POST['password'];
$result = array();
$result = $db->getAllJobs($email);
echo json_encode($result);
}
Job class that should be returned from php/sql query:
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;
}
}
And finally, the Java code from class CloudConnect:
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;
}