2012-07-10 133 views
0

我一直在这里一直保持现在,但我无法弄清楚为什么数据将被调用到应用程序,但Json无法将其转换。 它在logcat中显示JSONObject不能转换为JSONArray。Android:通过Json使用PHP解析MySql数据库的问题

赫雷什类:

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 

回答

0
JSONArray jArray = new JSONArray(data); 

应该是

JSONObject jArray = new JSONObject(data); 

您可以从JSONObject中拉出您的数组。

+0

我试过了,但我得到了一个LogCat JSONArray无法转换为JSONObject。 – user1509521 2012-07-10 19:42:52

+0

您可以发布您的JSON字符串吗? – zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz 2012-07-10 21:29:31