Android:使用PHP通过Json解析MySql数据库时出现问题
我已经这样做了很长时间了,但是我不明白为什么数据会被调用到应用程序中,但是Json不能转换它。 它在logcat中显示JSONObject不能转换为JSONArray 这是课程:Android:使用PHP通过Json解析MySql数据库时出现问题,php,android,mysql,json,Php,Android,Mysql,Json,我已经这样做了很长时间了,但是我不明白为什么数据会被调用到应用程序中,但是Json不能转换它。 它在logcat中显示JSONObject不能转换为JSONArray 这是课程: public class SqlTest extends Activity { public String data; public List<String> suggest; public AutoCompleteTextView autoComplete; public ArrayAdapter<
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;
}
}
}
应该是
JSONObject jArray = new JSONObject(data);
您可以从JSONObject中提取数组。我已经尝试过了,但是我得到一个LogCat JSONArray无法转换为JSONObject。请发布您的JSON字符串好吗?
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
JSONArray jArray = new JSONArray(data);
JSONObject jArray = new JSONObject(data);