I've this php that returns an array with values from database:
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);
}
And I don't know how can I receive this array on JAVA, because this array have no name... I've tried this:
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
But it's throwing an exception:
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)
Any idea ? Thanks