1

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;
}
fakataha
  • 683
  • 5
  • 30

1 Answers1

2

I really do not know PHP, but Gson message is quite clear to me. First char of your JSON is not { or [ and this is a required condition for the string to be a valid JSON.

So check you PHP code and verify that your response starts with { (since I can wonder that your code is returning a single object)

For JSON Specification:

  1. JSON Grammar

    A JSON text is a sequence of tokens. The set of tokens includes six structural characters, strings, numbers, and three literal names.

    A JSON text is a serialized object or array.

    JSON-text = object / array

giampaolo
  • 6,648
  • 5
  • 42
  • 73
  • are you referring to the array results? or the json_encode method? I've updated the code to reflect what I think is correct, but I have no idea really with PHP or Json. – fakataha Oct 12 '13 at 07:02
  • To help you, just edit your question with content of is stream. If you do not know how to do, look here for example: http://stackoverflow.com/a/309718/1360888 – giampaolo Oct 12 '13 at 07:32
  • This is what eventually helped me work out the problem. something along the lines you suggested but wasn't sure how to display the input in Eclipse. String all = EntityUtils.toString(entity); Log.d("response", all); allowed me to see that the errors the php was throwing and finally the json format so i can verify it was what i wanted. – fakataha Oct 15 '13 at 22:50
  • Glad you solved. Mark as accepted if you think it helped you to solve, don't mark otherwise :) – giampaolo Oct 16 '13 at 05:47