我正在尝试执行 autocompleteTextView。我正在尝试 Wiki 示例。对于 Wiki,它可以工作。但是对于我自己的 api 它不起作用。我正在尝试按姓氏打电话。我尝试使用 jSonObject。但看起来我犯了一些错误。这是我的代码。
public class WikiSuggestActivity 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.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://en.wikipedia.org/w/api.php?action=opensearch&search="+newText+"&limit=8&namespace=0&format=json");
HttpGet hGet = new HttpGet("http://api.xyz.com?response_format=json&version=2.0&name="+newText);
ResponseHandler<String> rHandler = new BasicResponseHandler();
data = hClient.execute(hGet,rHandler);
suggest = new ArrayList<String>();
JSONArray jArray = new JSONArray(data);
JSONObject mJsonObject = new JSONObject();
for(int i=0;i<jArray.getJSONArray(1).length();i++){
String SuggestKey = jArray.getJSONArray(1).getString(i);
// mJsonObject = jArray.getJSONObject(i);
// mJsonObject.getString("lastname");
suggest.add(SuggestKey);
}
}catch(Exception e){
Log.w("Error", e.getMessage());
}
return null;
}
public void onPostExecute(Void result) {
aAdapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.item,suggest);
autoComplete.setAdapter(aAdapter);
aAdapter.notifyDataSetChanged();
}
}
}
这是我的 JSON,它是一个有效的 JSON
{
"body": {
"players": [
{
"firstname": "abc",
"lastname": "def"
},
{
"firstname": "xyz",
"lastname": "abc"
},
]
},
"statusCode": 200
}