简体   繁体   中英

AutoCompleteText View using webservice api Giving me error in Android

I am trying to do the autocompleteTextView. I am trying the Wiki Example. For Wiki it works. But for my own api its not working. I am trying to call by lastname. I tried using the jSonObject. But looks like i am making some mistake. Here is my Code.

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

   }
}

Here is my JSON and its a valid JSON

{
"body": {
    "players": [
        {
            "firstname": "abc",
            "lastname": "def"
        },
        {
            "firstname": "xyz",
            "lastname": "abc"
        },
  ]
},
"statusCode": 200

}

Edit:

Parse your json like this

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

You can Capture the results from the doInBackGround by returning the result and use it in the onPostExecute .

You are trying to update UI from non UI thread, because doInBackground not running in the UI thread.

put thsi code in onPostExecute

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

You can get the suggest by returning the value in doInBackground

Take your doInBackground of type ArrayList and return the suggest

you'll the suggest in the onPostExecute as a method parameter, pass it to the adapter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM