我有一个使用volley进入AutoCompleteAdapter
的服务器的搜索请求,我希望将我的请求的结果输入到适配器中的结果过滤器但是在从服务器获取结果之前获取null到适配器和应用程序崩溃中的结果过滤器,我可以处理这种情况吗?谢谢,这是我的代码:
public class AutoCompleteAdapter extends ArrayAdapter<Company> implements Filterable {
private ArrayList<String> mData;
RequestQueue queue;
Context context ;
public AutoCompleteAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
mData = new ArrayList<>();
this.context = context ;
}
@Override
public int getCount() {
return mData.size();
}
@Override
public Filter getFilter() {
Filter myFilter = new Filter() {
@Override
protected FilterResults performFiltering(final CharSequence constraint) {
FilterResults filterResults = new FilterResults();
if(constraint != null) {
Log.e("constraint",constraint+"");
queue = Volley.newRequestQueue(context);
String url = "http://eteebar.com/api/main/search";
// Request a string response from the provided URL.
StringRequest jsonArrayRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
ArrayList<Image> imagesArray;
ArrayList<Articles> articlesArray;
ArrayList<Comment> commentsArray;
ArrayList<Offer> offersArray;
try {
ArrayList<String> companies = new ArrayList<>();
JSONArray jsonArray = new JSONArray(response);
for(int i= 0 ; i < jsonArray.length(); i++){
imagesArray = new ArrayList<>();
articlesArray = new ArrayList<>();
commentsArray = new ArrayList<>();
offersArray = new ArrayList<>();
JSONObject c = jsonArray.getJSONObject(i);
JSONArray images = c.getJSONArray("images") ;
JSONArray articles = c.getJSONArray("articles") ;
JSONArray comments = c.getJSONArray("comments") ;
JSONArray offers = c.getJSONArray("offers") ;
for(int img = 0 ; img < images.length() ; img ++){
try{
JSONObject im = images.getJSONObject(img);
Image image = new Image(im.getString("id"),im.getString("company_id"),im.getString("link"),
im.getString("is_deleted"));
imagesArray.add(img,image);
}
catch (JSONException ex){
}
}
for(int art = 0 ; art < articles.length() ; art ++){
try{
JSONObject ar = articles.getJSONObject(art);
Articles article = new Articles(ar.getString("id"),ar.getString("company_id"),ar.getString("link"),ar.getString("subject"),
ar.getString("description"),ar.getString("is_active"));
articlesArray.add(art,article);
}
catch (JSONException ex){
}
}
for(int com = 0 ; com < comments.length() ; com ++){
try{
JSONObject commen = comments.getJSONObject(com);
Comment comment = new Comment(commen.getString("id"),commen.getString("comment"),commen.getString("company_id"),
commen.getString("customer_id"),commen.getString("plus_count"),commen.getString("minus_count"),commen.getString("is_active")
,commen.getString("is_deleted"),commen.getString("customer_name"));
commentsArray.add(com,comment);
}
catch (JSONException ex){
}
}
for(int off = 0 ; off < offers.length() ; off ++){
try{
JSONObject of = offers.getJSONObject(off);
Offer offer = new Offer(of.getString("id"),of.getString("customer_id"),of.getString("keyfiate_mahsoulat"),
of.getString("pasokhgouyi"),of.getString("poshtibani"),of.getString("takhassos"),of.getString("tarrahiye_basari"),of.getString("tabliqat"),
of.getString("tarefeha"),of.getString("qedmat"),of.getString("sorat"),of.getString("standard"),of.getString("peygiri"),
of.getString("bastebandi"),of.getString("website"),of.getString("amouzesh"),of.getString("berouz_boudan"),of.getString("vosate_brand"),of.getString("vabaste_be_dolat"),
of.getString("description"),of.getString("description_balance"),of.getString("is_deleted"),of.getString("customer_name"));
offersArray.add(off,offer);
}
catch (JSONException ex){
}
}
Company company = new Company(c.getString("id"),c.getString("customer_id"),c.getString("name"),c.getString("brand"),c.getString("shoar"),c.getString("email"),c.getString("website"),c.getString("employees_count"),c.getString("branchs_count"),
c.getString("tags"),c.getString("customers_count"),c.getString("product_id1"),c.getString("product_id2"),c.getString("product_id3"),c.getString("telegram"),c.getString("instagram"),
c.getString("facebook"),c.getString("linkedin"),c.getString("google"),c.getString("mazaya"),c.getString("goals"),c.getString("description"),c.getString("link"),c.getString("seen_count"),c.getString("comment_balance"),c.getString("fields_balance"),c.getString("customer_balance"),c.getString("etebar_balance"),c.getString("main_balance")
,c.getString("sharing_count"),c.getString("is_active"),c.getString("is_deleted"),c.getString("created_at_jalali"),c.getString("updated_at_jalali"),c.getString("created_at"),c.getString("updated_at"),c.getString("category_name"),c.getString("customer_name"),c.getString("product_name1"),c.getString("product_name2"),c.getString("product_name3"),imagesArray,articlesArray,commentsArray,offersArray);
companies.add(company.name);
}
Log.e("companySize:",companies.size()+"");
if(companies.size()>0){
FilterResults filter = new FilterResults();
mData = companies ;
Log.e("mData",mData.size()+" " + mData);
filter.values = mData;
filter.count = mData.size();
// Log.e("filterResult",filterResults.values+"");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Volley",error.toString());
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
Log.e("searchText",constraint.toString());
if(constraint.length()>0){
params.put("key", constraint.toString());
}
params.put("offset", "0");
params.put("limit","20");
return params;
}
};
jsonArrayRequest.setTag("tag");
jsonArrayRequest.setRetryPolicy(new DefaultRetryPolicy(
400000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
// Add the request to the RequestQueue.
queue.add(jsonArrayRequest);
// Now assign the values and count to the FilterResults object
}
return filterResults;
}
@Override
protected void publishResults(CharSequence contraint, FilterResults results) {
if(results != null && results.count > 0) {
notifyDataSetChanged();
}
else {
notifyDataSetInvalidated();
}
}
};
return myFilter;
}
}
答案 0 :(得分:2)
在辅助线程中调用方法performFiltering()
。你再次通过Volley进行异步调用。因此,方法performFiltering()
在响应到达之前返回,从而导致崩溃。您只需阻止performFiltering()
,直到响应到来。
这可以通过Volley提供的RequestFuture
来实现。
queue = Volley.newRequestQueue(context);
String url = "http://eteebar.com/api/main/search";
RequestFuture<String> future = RequestFuture.newFuture();
StringRequest request = new StringRequest(Method.POST, url, reqBody, future, future);
queue.add(request);
try {
String response = future.get(); // this is a blocking call
// write your body of onResponse() here
} catch (InterruptedException e) {
} catch (ExecutionException e) {
// if something went wrong
}