0

我一直在做这个,但我不知道为什么数据会被调用到应用程序,但 Json 无法转换它。它在 logcat 中显示 JSONObject 无法转换为 JSONArray。

继承人类:

public class SqlTest extends Activity {
public String data;
public List<String> suggest;
public AutoCompleteTextView autoComplete;
public ArrayAdapter<String> aAdapter;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sqltest);
    suggest = new ArrayList<String>();
    autoComplete = (AutoCompleteTextView) findViewById(R.id.autocomplete_country);
    autoComplete.addTextChangedListener(new TextWatcher(){

        public void afterTextChanged(Editable editable) {
            //

        }

        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            //

        }

        public void onTextChanged(CharSequence s, int start, int before, int count) {
            String newText = s.toString();
            new getJson().execute(newText);
        }

    });

}
   class getJson extends AsyncTask<String,String,String>{

@Override
protected String doInBackground(String... key) {
    String newText = key[0];
    newText = newText.trim();
    newText = newText.replace(" ", "+");
    try{
        HttpClient hClient = new DefaultHttpClient();
        HttpGet hGet = new HttpGet("http://android.mojavenuclear.com/androidscript.php");
        ResponseHandler<String> rHandler = new BasicResponseHandler();
        data = hClient.execute(hGet,rHandler);
        suggest = new ArrayList<String>();
        JSONArray jArray = new JSONArray(data);
        for(int i=0;i<jArray.getJSONArray(1).length();i++){
        String SuggestKey = jArray.getJSONArray(1).getString(i);
        suggest.add(SuggestKey);
        }

    }catch(Exception e){
        Log.w("Error", e.getMessage());
    }
    runOnUiThread(new Runnable(){
        public void run(){
             aAdapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.list_item,suggest);
             autoComplete.setAdapter(aAdapter);
             aAdapter.notifyDataSetChanged();
        }
    });

    return null;
    }

   }
}

和 PHP:

<?php
mysql_connect("","","");
mysql_select_db("androiddatastore");
$desc = mysql_real_escape_string($_REQUEST['newText']); 
 $q = mysql_query("SELECT `Shrt_Desc`, `Carbohydrt_(g)` FROM `Abbrev` WHERE `Shrt_Desc` LIKE '$desc%' LIMIT 0, 30"); 
while($e=mysql_fetch_assoc($q))
    $output[]=$e;
print(json_encode($output));
mysql_close();
?>

LogCat 输出:

07-10 11:57:20.872: W/KeyCharacterMap(15730): Using default     keymap: /system/usr/keychars/qwerty.kcm.bin
07-10 11:58:37.948: W/Error(15730): Value {"Carbohydrt_(g)":"0.06","Shrt_Desc":"BUTTER,WHIPPED,WITH SALT"} at 1 of type org.json.JSONObject cannot be   converted to JSONArray
07-10 11:58:38.358: W/Error(15730): Value {"Carbohydrt_(g)":"0.06","Shrt_Desc":"BUTTER,WHIPPED,WITH SALT"} at 1 of type org.json.JSONObject cannot be converted to JSONArray
07-10 11:58:38.468: W/Error(15730): Value {"Carbohydrt_(g)":"0.06","Shrt_Desc":"BUTTER,WHIPPED,WITH SALT"} at 1 of type org.json.JSONObject cannot be converted to JSONArray
07-10 11:58:38.818: W/Error(15730): Value {"Carbohydrt_(g)":"0.06","Shrt_Desc":"BUTTER,WHIPPED,WITH SALT"} at 1 of type org.json.JSONObject cannot be converted to JSONArray
4

1 回答 1

0
JSONArray jArray = new JSONArray(data);

应该

JSONObject jArray = new JSONObject(data);

您可以从JSONObject中提取您的数组。

于 2012-07-10T19:16:56.243 回答