期望BEGIN_ARRAY,但在第1行第1列是STRING:用Gson从PHP到Java获取JSON


Expected BEGIN_ARRAY but was STRING at line 1 column 1: getting JSON from PHP to Java with Gson

我的代码有问题。我想从编码JSON数组的PHP脚本中获取值;

我的PHP代码:
<?php
$roro = array();
$roro[]  = array(
      'id' => '1',
      'title' => 'title1'
   );
$roro[] = array(
      'id' => '2',
      'title' => 'title2'
   );
$out = array_values($roro);
 echo json_encode($out);
?>

这是我的Java代码解码JSON:

    HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost("http://www.liliandgizmo.legtux.org/lilyCreation/getCategorie.php");
        // post.setEntity(new UrlEncodedFormEntity(validateMessage(m)));
        HttpResponse response = client.execute(post);
        StatusLine status = response.getStatusLine();
        if (status.getStatusCode() == 200) {
            HttpEntity entity = response.getEntity();
            InputStream is = entity.getContent();
            final Gson gson = new GsonBuilder().create();
            try {
                Reader read = new InputStreamReader(is);
                mList = gson.fromJson(read, new TypeToken<List<Categorie>>() {
                }.getType());
                is.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

my category class:

public class Categorie {
/**
 * Identifiant en DB.
 */
private int id;
/**
 * Nom de la catégorie.
 */
private String title;
    // + getter & setter
}

我的问题:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 1
    at com.google.gson.Gson.fromJson(Gson.java:815)
    at com.google.gson.Gson.fromJson(Gson.java:768)
    at fr.gizmoichitzo.loisirsaurelie.upload.shared.LoisirGetCategory.test(LoisirGetCategory.java:50)
    at fr.gizmoichitzo.loisirsaurelie.upload.client.Main.main(Main.java:22)
Caused by: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 1
    at com.google.gson.stream.JsonReader.beginArray(JsonReader.java:338)
    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:79)
    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:60)
    at com.google.gson.Gson.fromJson(Gson.java:803)
    ... 3 more

如果我理解我的问题,我认为JSON必须解码应该看起来像:

'[{...},{...}]'

但是…为什么没有'就不像[{...},{...}] ??

如果我在浏览器上播放脚本,我得到[{"id":"1","title":"title1"},{"id":"2","title":"title2"}]

我真的不懂PHP,但是用Java你可以添加一些"调试"到你的代码,像这样:

        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost("http://www.liliandgizmo.legtux.org/lilyCreation/getCategorie.php");
        // post.setEntity(new UrlEncodedFormEntity(validateMessage(m)));
        HttpResponse response = client.execute(post);
        StatusLine status = response.getStatusLine();
        if (status.getStatusCode() == 200) {
            HttpEntity entity = response.getEntity();
            InputStream is = entity.getContent();
            final Gson gson = new GsonBuilder().create();
            try {
                Reader read = new InputStreamReader(is);
                BufferedReader in = new BufferedReader(read); //added
                String inputLine; //added
                while ((inputLine = in.readLine()) != null) //added
                    System.out.println(inputLine); //added
                in.close(); //added
                //List<Categorie> mList = gson.fromJson(read, new TypeToken<List<Categorie>>() {
                }.getType());
                is.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

,你会看到脚本返回的字符串是:

[{"id":"1","title":"title1"},{"id":"2","title":"title2"}]

当然,这是一个无效的JSON,所以Gson会抛出异常。