我正在尝试使用json使简单的应用程序将数据从mysql加载到android。
这是我的PHP代码:

$response = array();
$result = mysql_query("SELECT * FROM Demo LIMIT 2");
if(mysql_num_rows($result) !=0)
{
    while($row = mysql_fetch_array($result)){
        array_push($response, new Image(
        $row["id"],
        $row["name"],
        $row["image"]
    ));
    }
echo json_encode($response);
}

class Image
{
 var $id;
    var $name;
    var $link1;

function Image($id, $name, $link1)
{
    $this->id = $id;
    $this->name = $name;
    $this->link1 = $link1;
}
}


结果json:


  [{{id“:” 1“,” name“:” thisisname“,” link1“:” thisislink“},{” id“:” 2“,” name“:” thisisname“,” link1“:” thisislink “}]


这是android代码:

class getDetailProduct extends AsyncTask<String, Integer, String> {

    @Override
    protected String doInBackground(String... strings) {

        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost("http://10.0.3.2/demo/display.php");

        String kq = "";
        try {
            HttpResponse response = httpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();
            kq = EntityUtils.toString(entity);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return kq;
    }

    @Override
    protected void onPostExecute(String s) {
        Log.e("result", s);
        try {
            JSONArray jsarr = new JSONArray(s); // Error this line
            if(jsarr.length()>0){
                for (int i = 0; i<jsarr.length(); i++){
                    JSONObject jo = jsarr.getJSONObject(i);
                    imageArrayList.add(new MyImage(
                            jo.getInt("id"),
                            jo.getString("name"),
                            jo.getString("link1")
                    ));
                }
                MyGridViewAdapter myGridViewAdapter = new MyGridViewAdapter(getApplicationContext(), R.layout.item_gridview, imageArrayList);
                gridView.setAdapter(myGridViewAdapter);
            }


        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}


这是Logcat:

    20:22:57.055 12905-12905/com.example.chientran.demoproject E/result: [{"id":"1","name":"thisisname","link1":"thisislink"},{"id":"2","name":"thisisname","link1":"thisislink"}]
 W/System.err: org.json.JSONException: Value  of type java.lang.String cannot be converted to JSONArray
W/System.err:     at org.json.JSON.typeMismatch(JSON.java:111)
W/System.err:     at org.json.JSONArray.<init>(JSONArray.java:96)
W/System.err:     at org.json.JSONArray.<init>(JSONArray.java:108)
W/System.err:     at com.example.chientran.demoproject.MainActivity$getDetailProduct.onPostExecute(MainActivity.java:93)
 W/System.err:     at com.example.chientran.demoproject.MainActivity$getDetailProduct.onPostExecute(MainActivity.java:57)
W/System.err:     at android.os.AsyncTask.finish(AsyncTask.java:632)
W/System.err:     at android.os.AsyncTask.access$600(AsyncTask.java:177)
W/System.err:     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
W/System.err:     at android.os.Looper.loop(Looper.java:135)
W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5221)
W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
W/System.err:     at java.lang.reflect.Method.invoke(Method.java:372)
W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)


但是如果我使用hardjson,它就可以工作

String ss= "[{\"id\":\"1\",\"name\":\"thisisname\",\"link1\":\"thisislink\"},{\"id\":\"2\",\"name\":\"thisisname\",\"link1\":\"thisislink\"}]";
JSONArray jsonarr  = new JSONArray(ss);// It work


`

最佳答案

       JSONArray arrayresult = ss.getJSONArray(response);

                for (int i = 0; i < arrayresult.length(); i++) {
                    JSONObject a = arrayresult.getJSONObject(i);

                        YourModel model = new YourModel();
                        model.id = a.getInt("id"),
                        model.name = a.getString("name"),
                        model.link1 = a.getString("link1")

                        modellist.add(model);
                 }

10-07 15:40