0

私はこれを永遠に続けてきましたが、なぜデータがアプリに呼び出されるのかわかりませんが、Json はそれを変換できません。JSONObject を JSONArray に変換できないことが logcat に示されています。

クラスは次のとおりです。

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 に答える