0

我正在尝试执行 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

}

4

1 回答 1

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);
}

您可以通过返回结果从 doInBackGround 捕获结果并在 onPostExecute 中使用它。

您正在尝试从非 UI 线程更新 UI,因为 doInBackground 未在 UI 线程中运行。

把这个代码放在 onPostExecute

  aAdapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.item,suggest);
             autoComplete.setAdapter(aAdapter);
             aAdapter.notifyDataSetChanged();

您可以通过返回 doInBackground 中的值来获得建议

获取 ArrayList 类型的 doInBackground 并返回建议

您将在 onPostExecute 中建议作为方法参数,将其传递给适配器

于 2013-03-08T16:05:29.423 回答