0

我想要做autocompleteTextView。我正在嘗試Wiki示例。對於Wiki來說,它的作用但對於我自己的api而言,它不起作用。我想用姓氏來打電話。我嘗試使用jSonObject。但看起來我犯了一些錯誤。這是我的代碼。AutoCompleteText使用webservice api查看在Android中給我的錯誤

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

什麼是錯誤的傢伙... – FWeigl 2013-03-08 15:49:45

+0

它會在catch異常... – dhiku 2013-03-08 15:52:35

+0

然後把一個Log.e(TAG,e.toString());在那裏,讓我們知道它說了什麼。 – FWeigl 2013-03-08 15:53:37

回答

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中建議作爲方法參數,將它傳遞給適配器

+0

仍然是我得到org.json.JSONException的同樣問題。 – dhiku 2013-03-08 16:09:48

+0

@DhiwakarMani用你的json字符串更新你的問題看起來你的json無效或者你沒有正確閱讀 – Pragnani 2013-03-08 16:10:38

+0

@DhiwakarMani,但是不管你有什麼異常,都要做上述修改。 – Pragnani 2013-03-08 16:11:02