1
我想從與外部數據庫進行通信的php中獲取JSON的微調框。我不斷收到JSON異常錯誤。如何使用JSON從外部數據庫填充微調框
我想獲得一個下拉菜單中,我可以選擇的寵物類型(即狗,貓,魚等)
這是JSON我從JSONParser
得到:
{"success":1,"0":{"pets_kind":"dog"},"1":{"pets_kind":"cat"}}
這是我的PHP:
DB_Functions.php
:
public function getPetKind(){
$result = array();
$fetch = mysql_query("SELECT pets_kind FROM PetsList") or die(mysql_error());
while ($row = mysql_fetch_array($fetch, MYSQL_ASSOC)) {
$row_array['pets_kind'] = $row['pets_kind'];
array_push($result,$row_array);
}
return $result;
}
的index.php:
}else if($tag == 'getpetkind'){
$pets_kind = array();
$pets_kind = $db->getPetKind();
if($pets_kind != false){
$response = array();
$response["success"]=1;
foreach ($pets_kind as $row) {
$row_array['pets_kind'] = $row['pets_kind'];
array_push($response,$row_array);
}
echo json_encode($response);
}
填充下拉的AsyncTask我在OnCreate()
撥打:
private class FillDropDown extends AsyncTask<String,Void,JSONObject>{
@Override
protected void onPreExecute(){
super.onPreExecute();
petsKindDropDown = (Spinner) findViewById(R.id.petsKindDropDown);
}
@Override
protected JSONObject doInBackground(String... args){
UserFunctions userFunctions = new UserFunctions();
JSONObject json = userFunctions.getPetsKind();
return json;
}
@Override
protected void onPostExecute(JSONObject json){
/**
* Checks for success message
*/
try{
if(json.getString(KEY_SUCCESS) != null){
ArrayList<String> kind = new ArrayList<String>();
JSONArray jsonArr = new JSONArray(json.optString("pets_kind"));
for(int i = 0; i < jsonArr.length(); i++){
JSONObject jsonObject = jsonArr.getJSONObject(i);
kind.add(jsonObject.optString("pets_kind"));
}
petsKindDropDown.setAdapter(new ArrayAdapter<String>(AddPet.this,
android.R.layout.simple_spinner_dropdown_item,kind));
}
else{
addPetAlertText.setText("Error occured in Filling Drop down");
}
} catch (JSONException e){
e.printStackTrace();
}
}
}
錯誤:
W/System.err: org.json.JSONException: End of input at character 0 of
W/System.err: at org.json.JSONTokener.syntaxError(JSONTokener.java:449)
W/System.err: at org.json.JSONTokener.nextValue(JSONTokener.java:97)
W/System.err: at org.json.JSONArray.<init>(JSONArray.java:92)
W/System.err: at org.json.JSONArray.<init>(JSONArray.java:108)
W/System.err: at com.adicili.petdetective.AddPet$FillDropDown.onPostExecute(AddPet.java:133)
W/System.err: at com.adicili.petdetective.AddPet$FillDropDown.onPostExecute(AddPet.java:103)
W/System.err: at android.os.AsyncTask.finish(AsyncTask.java:651)
W/System.err: at android.os.AsyncTask.-wrap1(AsyncTask.java)
W/System.err: at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
W/System.err: at android.os.Looper.loop(Looper.java:148)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5417)
W/System.err: at java.lang.reflect.Method.invoke(Native Method)
W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
太謝謝你了! – Adicili
你歡迎我的朋友 – meda