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;
}

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

4 Comments

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.
To help you, just edit your question with content of is stream. If you do not know how to do, look here for example: stackoverflow.com/a/309718/1360888
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.
Glad you solved. Mark as accepted if you think it helped you to solve, don't mark otherwise :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.