gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 19:16:15 25 4
gpt4 key购买 nike

我正在尝试使用 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);
}

关于php - java.lang.String 类型的值无法转换为 JSONArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42685174/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com