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();
}
}
答案 0 :(得分:3)
result
是JSONObject
而不是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实施,执行流程意味着在建立互联网连接时没有给出许可。
答案 1 :(得分:0)
JSONObject obje = new JSONObject(results);
JSONArray jArray = obje.getJSONArray("data");
然后就像你一样。 只记得对象的{}和JSON的数组的[]
答案 2 :(得分:0)
您正在将字符串转换为jsonArray,其中您的响应是jsonObject:
这样做:
JSONObject jsnobject = new JSONObject(result);
JSONArray jsonArray = jsnobject.getJSONArray("data");
for (int i = 0; i < jsonArray.length(); i++)
{
JSONObject data= jsonArray.getJSONObject(i);
dataList.add(data.getString("name"));
}