Android 如何使用mySQL和Json在AutoCompleteTextView中获取数据?
我用这个密码试过了 MainActivity.javaAndroid 如何使用mySQL和Json在AutoCompleteTextView中获取数据?,android,mysql,json,Android,Mysql,Json,我用这个密码试过了 MainActivity.java public class MainActivity extends Activity { public String data; public List<String> suggest; public AutoCompleteTextView autoComplete; public ArrayAdapter<String> aAdapter; /** Called when
public class MainActivity 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.activity_main);
suggest = new ArrayList<String>();
autoComplete = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
autoComplete.addTextChangedListener(new TextWatcher(){
public void afterTextChanged(Editable editable) {
// TODO Auto-generated method stub
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
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://10.0.2.2/autocmplt/abc.php?name=");
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.item,suggest);
autoComplete.setAdapter(aAdapter);
aAdapter.notifyDataSetChanged();
}
});
return null;
}
}
}
<AutoCompleteTextView
android:id="@+id/autoCompleteTextView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="60dp"
android:ems="13"
android:hint="AutoCompleteTextView" >
<requestFocus />
</AutoCompleteTextView>
</LinearLayout>
这就是我正在努力的方式
<AutoCompleteTextView
android:id="@+id/autoCompleteTextView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="60dp"
android:ems="13"
android:hint="AutoCompleteTextView" >
<requestFocus />
</AutoCompleteTextView>
</LinearLayout>
像这样得到错误
<AutoCompleteTextView
android:id="@+id/autoCompleteTextView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="60dp"
android:ems="13"
android:hint="AutoCompleteTextView" >
<requestFocus />
</AutoCompleteTextView>
</LinearLayout>
08-17 18:14:33.745 5284-5307/com.example.sachin.autocomplete W/Error: Value {"results":[{"id":"1","name":"sachin"},{"id":"2","name":"solanki"},{"id":"3","name":"abc"},{"id":"4","name":"abcd"}]} of type org.json.JSONObject cannot be converted to JSONArray
您确定正确解析了JSON吗?除非您发布的返回字符串不正确,否则您试图解析它的代码会出现异常。我建议使用带有POJO的json框架:
<AutoCompleteTextView
android:id="@+id/autoCompleteTextView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="60dp"
android:ems="13"
android:hint="AutoCompleteTextView" >
<requestFocus />
</AutoCompleteTextView>
</LinearLayout>
以下第一个测试失败,第二个测试成功:
<AutoCompleteTextView
android:id="@+id/autoCompleteTextView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="60dp"
android:ems="13"
android:hint="AutoCompleteTextView" >
<requestFocus />
</AutoCompleteTextView>
</LinearLayout>
@Test
public void testJson() {
String data = "{\"results\":[{\"id\":\"1\",\"name\":\"sachin\"},{\"id\":\"2\",\"name\":\"solanki\"},{\"id\":\"3\",\"name\":\"abc\"},{\"id\":\"4\",\"name\":\"abcd\"}]}";
JSONArray jArray = null;
ArrayList<String> suggest = new ArrayList<>();
try {
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 (JSONException e) {
e.printStackTrace();
}
}
@Test
public void testJson2() {
String data = "{\"results\":[{\"id\":\"1\",\"name\":\"sachin\"},{\"id\":\"2\",\"name\":\"solanki\"},{\"id\":\"3\",\"name\":\"abc\"},{\"id\":\"4\",\"name\":\"abcd\"}]}";
ArrayList<String> suggest = new ArrayList<>();
Gson gson = new Gson();
Results r = gson.fromJson(data, Results.class);
for ( Custom c : r.customArray ) {
suggest.add(c.name);
}
}
class Results {
@SerializedName("results")
@Expose
public ArrayList<Custom> customArray;
}
class Custom {
@SerializedName("id")
@Expose
public String id;
@SerializedName("name")
@Expose
public String name;
}
@测试
public void testJson(){
字符串数据=“{\'results\”:[{\'id\':\'1\',\'name\':\'sachin\',{\'id\':\'2\',\'name\':\'solanki\',{\'id\':'3\',\'name\':\'abc\',{\'id\':'4\',\'name\':'abcd\';
JSONArray-jArray=null;
ArrayList suggest=新建ArrayList();
试一试{
jArray=新的JSONArray(数据);
对于(int i=0;i您确定正确解析了JSON吗?除非您发布的返回字符串不正确,否则您尝试解析它的代码会出现异常。我建议改用带有POJO的JSON框架:
<AutoCompleteTextView
android:id="@+id/autoCompleteTextView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="60dp"
android:ems="13"
android:hint="AutoCompleteTextView" >
<requestFocus />
</AutoCompleteTextView>
</LinearLayout>
以下第一个测试失败,第二个测试成功:
<AutoCompleteTextView
android:id="@+id/autoCompleteTextView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="60dp"
android:ems="13"
android:hint="AutoCompleteTextView" >
<requestFocus />
</AutoCompleteTextView>
</LinearLayout>
@Test
public void testJson() {
String data = "{\"results\":[{\"id\":\"1\",\"name\":\"sachin\"},{\"id\":\"2\",\"name\":\"solanki\"},{\"id\":\"3\",\"name\":\"abc\"},{\"id\":\"4\",\"name\":\"abcd\"}]}";
JSONArray jArray = null;
ArrayList<String> suggest = new ArrayList<>();
try {
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 (JSONException e) {
e.printStackTrace();
}
}
@Test
public void testJson2() {
String data = "{\"results\":[{\"id\":\"1\",\"name\":\"sachin\"},{\"id\":\"2\",\"name\":\"solanki\"},{\"id\":\"3\",\"name\":\"abc\"},{\"id\":\"4\",\"name\":\"abcd\"}]}";
ArrayList<String> suggest = new ArrayList<>();
Gson gson = new Gson();
Results r = gson.fromJson(data, Results.class);
for ( Custom c : r.customArray ) {
suggest.add(c.name);
}
}
class Results {
@SerializedName("results")
@Expose
public ArrayList<Custom> customArray;
}
class Custom {
@SerializedName("id")
@Expose
public String id;
@SerializedName("name")
@Expose
public String name;
}
@测试
public void testJson(){
字符串数据=“{\'results\”:[{\'id\':\'1\',\'name\':\'sachin\',{\'id\':\'2\',\'name\':\'solanki\',{\'id\':'3\',\'name\':\'abc\',{\'id\':'4\',\'name\':'abcd\';
JSONArray-jArray=null;
ArrayList suggest=新建ArrayList();
试一试{
jArray=新的JSONArray(数据);
对于(int i=0;我很抱歉,先生,我没有明白这一点。这是我的第一个项目,对这件事有点困惑。这些是用于测试独立代码部分的简单单元测试。第一个单元测试是在解析json字符串时抛出异常的代码。第二个测试是我建议的解决方案,用于解决失败的代码。我很抱歉对不起,先生,我没有明白这一点。这是我的第一个项目,对这件事有点困惑。这些是用于测试独立代码部分的简单单元测试。第一个单元测试是您在解析json字符串时抛出异常的代码。第二个测试是我针对失败代码提出的解决方案。