我正在尝试使用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
}
答案 0 :(得分:1)
编辑:
像这样解析你的json
JSONObject obj=new JSONObject(data);
JSONObject obj2=obj.getJSONObject("body");
JSONArray array=obj2.getJSONArray("players");
for(int i=0;i<array.length();i++)
{
JSONObject playerinfo=array.getJSONObject(i);
String lastname=playerinfo.getString("lastname");
suggest.add(lashname);
}
您可以通过返回结果并从onPostExecute中使用来捕获doInBackGround中的结果。
您正在尝试从非UI线程更新UI,因为doInBackground未在UI线程中运行。
将thsi代码放入onPostExecute
aAdapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.item,suggest);
autoComplete.setAdapter(aAdapter);
aAdapter.notifyDataSetChanged();
您可以通过返回doInBackground中的值来获取建议
获取ArrayList类型的doInBackground 并返回建议
你将onPostExecute中的建议作为方法参数,将其传递给适配器