【发布时间】:2014-02-25 23:14:17
【问题描述】:
我有这个 php,它返回一个包含数据库值的数组:
public function getHashTags(){
$res = array();
$i = 0;
$userID = $this->getUserID();
$db_ = $this->dbSelect("hashTags","userID = '$userID' ORDER BY hashTag ASC");
while($db = $this->dbAssoc($db_)){
$key = $db['id'];
$res[$key] = $db['hashTag'];
$i++;
}
return $this->genResponse($res);
}
而且我不知道如何在 JAVA 上接收这个数组,因为这个数组没有名字...... 我试过这个:
class LoadHashTags extends AsyncTask<String, String, String> {
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("token", token));
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(getHashTagsURL, "POST", params);
// Check your log cat for JSON response
Log.d("All hashTags: ", json.toString());
try { //Trying to get hashTags
JSONArray hashTagsArray = new JSONArray(json.toString());
// Looping through all videos
for (int i = 0; i < hashTagsArray.length(); i++) {
JSONObject c = hashTagsArray.getJSONObject(i);
int hashTagId = c.getInt("id");
String hashTagTxt = c.getString("hashTag");
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put("hashId", hashTagId);
map.put("hashTagTxt", hashTagTxt);
// adding HashList to ArrayList
hashTagsList.add(map);
} //End for
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(), "HashTags Loaded", Toast.LENGTH_LONG).show();
}
});
// dismiss the dialog after getting all hashTags
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
ListAdapter adapter = new SimpleAdapter(
VideoUpload.this, hashTagsList,
R.layout.hashTags_list_item, new String[] { "hashTagId", "hashTagTxt"},
new int[] { R.id.hashTagId, R.id.hashTagTxt}
);
// updating spinner
setListAdapter(adapter);
}
});
} //Close PostExecute
} // CLOSER LOAD HASHTAGS
但是它抛出了一个异常:
02-25 22:48:58.450: W/System.err(1639): org.json.JSONException: Value {"270":"My hashTag test","272":"my hastag test2","271":"my hashtag test2","274":"my hashtag test5","273":"my hashtag test3"} of type org.json.JSONObject cannot be converted to JSONArray
02-25 22:48:58.450: W/System.err(1639): at org.json.JSON.typeMismatch(JSON.java:111)
02-25 22:48:58.460: W/System.err(1639): at org.json.JSONArray.<init> (JSONArray.java:96)
02-25 22:48:58.460: W/System.err(1639): at org.json.JSONArray.<init>(JSONArray.java:108)
02-25 22:48:58.460: W/System.err(1639): at com.example.kroosky.VideoUpload$LoadHashTags.doInBackground(VideoUpload.java:484)
02-25 22:48:58.480: W/System.err(1639): at com.example.kroosky.VideoUpload$LoadHashTags.doInBackground(VideoUpload.java:1)
02-25 22:48:58.480: W/System.err(1639): at android.os.AsyncTask$2.call(AsyncTask.java:288)
02-25 22:48:58.490: W/System.err(1639): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
02-25 22:48:58.490: W/System.err(1639): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
02-25 22:48:58.490: W/System.err(1639): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
02-25 22:48:58.490: W/System.err(1639): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
02-25 22:48:58.490: W/System.err(1639): at java.lang.Thread.run(Thread.java:841)
有什么想法吗?谢谢
【问题讨论】:
-
{} 不是 json 数组 ...
-
是的,但这是因为我将 toString() 用于 jsonObject,如果你看到 php,它会返回一个数组...
标签: java php android arrays json