java.lang.String类型的值无法转换为JSONArray

时间:2017-03-09 01:45:29

标签: php android mysql json

我正在尝试使用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""名称":" thisisname"" LINK1&#34 ;: " thisislink"},{" ID":" 2""名称":" thisisname"&#34 ; 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

`

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

好的,这将是你的JSON:

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

您应该在结果字符串中获得高于JSON的值。 现在,处理JSON:

try{
JSONArray jArray=new JSONArray(ss);
for(int i=0;i<jArray.length();i++){
JSONObject jObj=jArray.getJSONObject(i);
int id=Integer.parseInt(jObj.getString("id"));
String name=jObj.getString("name");
String link1=jObj.getString("link1");
 imageArrayList.add(new MyImage(
                            id,name,link1));
}

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

这应该有效

答案 2 :(得分:0)

感谢大家的支持。我已经解决了我的问题 我的android代码是正确的,但在文件php我使用UTF-8-BOM编码。所以我把它改成了UTF-8中的编码。它的工作