gpt4 book ai didi

java - org.json.JSONException 值 java.lang.String 类型的连接无法转换为 JSONArray

转载 作者:搜寻专家 更新时间:2023-11-01 08:29:55 25 4
gpt4 key购买 nike

JSON 数据:

{
"data": [
{
"id": 5,
"name": "Deal One"
},
{
"id": 6,
"name": "Deal Two"
},
{
"id": 7,
"name": "Deal Three"
}
]
}

onPostExecute,其中 JSONArray 被转换。

    @Override
protected void onPostExecute(String result) {

//this method will be running on UI thread
ArrayList<String> dataList = new ArrayList<String>();
pdLoading.dismiss();


if(result.equals("no rows")) {

// Do some action if no data from database

}else{

try {

JSONArray jArray = new JSONArray(result);

// Extract data from json and store into ArrayList
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
dataList.add(json_data.getString("name"));
}

strArrData = dataList.toArray(new String[dataList.size()]);

} catch (JSONException e) {
// You to understand what actually error is and handle it appropriately
Toast.makeText(MainActivity.this, e.toString(), Toast.LENGTH_LONG).show();
Toast.makeText(MainActivity.this, result.toString(), Toast.LENGTH_LONG).show();
}

}

}

所有这一切背后的重要理念是什么?在 this tutorial 之后进行自动完成演出.我注意到他们的 JSON 看起来与我的非常不同。

如何修复 JSONArray 错误?

EDIT2

STACKTRACE:(编辑 3:当我遵循其他人的代码时会发生以下错误)

01-19 14:41:15.442 6513-6513/gsi.xform W/System.err: org.json.JSONException: Value Connection of type java.lang.String cannot be converted to JSONObject
01-19 14:41:15.448 6513-6513/gsi.xform W/System.err: at org.json.JSON.typeMismatch(JSON.java:111)
01-19 14:41:15.448 6513-6513/gsi.xform W/System.err: at org.json.JSONObject.<init>(JSONObject.java:160)
01-19 14:41:15.448 6513-6513/gsi.xform W/System.err: at org.json.JSONObject.<init>(JSONObject.java:173)
01-19 14:41:15.448 6513-6513/gsi.xform W/System.err: at gsi.xform.MainActivity$AsyncFetch.onPostExecute(MainActivity.java:385)
01-19 14:41:15.448 6513-6513/gsi.xform W/System.err: at gsi.xform.MainActivity$AsyncFetch.onPostExecute(MainActivity.java:288)
01-19 14:41:15.448 6513-6513/gsi.xform W/System.err: at android.os.AsyncTask.finish(AsyncTask.java:651)
01-19 14:41:15.448 6513-6513/gsi.xform W/System.err: at android.os.AsyncTask.-wrap1(AsyncTask.java)
01-19 14:41:15.448 6513-6513/gsi.xform W/System.err: at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
01-19 14:41:15.448 6513-6513/gsi.xform W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
01-19 14:41:15.448 6513-6513/gsi.xform W/System.err: at android.os.Looper.loop(Looper.java:148)
01-19 14:41:15.448 6513-6513/gsi.xform W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5417)
01-19 14:41:15.448 6513-6513/gsi.xform W/System.err: at java.lang.reflect.Method.invoke(Native Method)
01-19 14:41:15.448 6513-6513/gsi.xform W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
01-19 14:41:15.448 6513-6513/gsi.xform W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

EDIT4:

这是 doInBackground 代码:

    @Override
protected String doInBackground(String... params) {
try {

// Enter URL address where your php file resides or your JSON file address
url = new URL("http://myprivate.url?X-Authorization=token");

} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return e.toString();
}
try {

// Setup HttpURLConnection class to send and receive data from php and mysql
conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(READ_TIMEOUT);
conn.setConnectTimeout(CONNECTION_TIMEOUT);
conn.setRequestMethod("GET");

// setDoOutput to true as we receive data
conn.setDoOutput(true);
conn.connect();

} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
return e1.toString();
}

try {

int response_code = conn.getResponseCode();

// Check if successful connection made
if (response_code == HttpURLConnection.HTTP_OK) {

// Read data sent from server
InputStream input = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
StringBuilder result = new StringBuilder();
String line;

while ((line = reader.readLine()) != null) {
result.append(line);
}

// Pass data to onPostExecute method
return (result.toString());

} else {
return("Connection error");
}

} catch (IOException e) {
e.printStackTrace();
return e.toString();
} finally {
conn.disconnect();
}


}

最佳答案

resultJSONObject 而不是 JSONArray

   // fetch the object      
JSONObject obj = new JSONObject(result);

// fetch the data array
JSONArray jArray = obj.getJSONArray("data");

// Extract data from json and store into ArrayList
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
dataList.add(json_data.getString("name"));
}

{                     // beginning of JSONObject
"data": [ // nested JSONArray
{ // nested JSONObject inside JSONArray
"id": 5,
"name": "Deal One"
},
{
"id": 6,
"name": "Deal Two"
},
{
"id": 7,
"name": "Deal Three"
}
]
} // ending of JSONObject

信息:另一个问题是由于Runtime-Permission执行流程的实现意味着在建立互联网连接时未给予许可。

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

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