{"id":4136,"date":"2018-02-26T16:35:36","date_gmt":"2018-02-26T11:05:36","guid":{"rendered":"https:\/\/www.innovationm.com\/blog\/?p=4136"},"modified":"2023-01-20T18:55:47","modified_gmt":"2023-01-20T13:25:47","slug":"synchronous-server-calling-within-filter","status":"publish","type":"post","link":"https:\/\/www.innovationm.com\/blog\/synchronous-server-calling-within-filter\/","title":{"rendered":"AutoCompleteView &#8211; Synchronous server call with Filter"},"content":{"rendered":"<p>In order to implement synchronous server calling within filter, <a href=\"https:\/\/developer.android.com\/reference\/android\/widget\/AutoCompleteTextView.html\">autocomplete<\/a> textview is being used. Web service is hit with each character typed in Autocomplete list. For having data dynamically in the list, we can make customized adapter. I have implemented it by making this adapter to implement filterable interface.<\/p>\n<p>For getting data from server, two approaches can be used:<\/p>\n<p>1) Using volley library:<\/p>\n<pre class=\"lang:java decode:true\">@Override\r\npublic Filter getFilter() {\r\nFilter filter = new Filter() {\r\n@Override\r\nprotected FilterResults performFiltering(CharSequence constraint){\r\nfinal FilterResults filterResults = new FilterResults();\r\nif (constraint != null) {\r\nJsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET,url,null,new com.android.volley.Response.Listener()\r\n{\r\n@Override\r\npublic void onResponse(JSONObject response) {\r\nResponseData responseData = new Gson().fromJson(response.toString(),ResponseData.class);\r\nif(responseData != null){\r\nfilterResults.values = responseData.getResponseData();\r\nfilterResults.count = responseData.getResponseData().size();\r\n}}},new com.android.volley.Response.ErrorListener() {\r\n@Override\r\npublic void onErrorResponse(VolleyError error) {\r\n}\r\n}) {\r\n@Override\r\npublic Map getHeaders() throws AuthFailureError {\r\nreturn getCustomHeaders();\r\n}};\r\n}\r\nreturn filterResults;\r\n}\r\n<\/pre>\n<p><span style=\"font-size: 1rem;\">Volley request can be made inside the performFiltering() function which gets callback after each character is typed. But the problem in this approach is due to asynchronous request , different thread is started and before getting any result, the execution of performFiltering() function stops. So we don&#8217;t receive result from server and before that publishResults() function is called to display the result to UI.<\/span><\/p>\n<p>2) Using Async Task:<\/p>\n<pre class=\"lang:java decode:true\">@Override\r\npublic Filter getFilter() {\r\nFilter filter = new Filter() {\r\n@Override\r\nprotected FilterResults performFiltering(CharSequence constraint) {\r\nfinal FilterResults filterResults = new FilterResults();\r\nif (constraint != null) {\r\nObject obj = WebServiceManager.callWebService();\r\nResponseData responseData = new Gson().fromJson(obj.toString(),ResponseData.class);\r\nif(responseData != null) {\r\nfilterResults.values = responseData.getResponseData();\r\nfilterResults.count = responseData.getResponseData().size();\r\n}}\r\nreturn filterResults;\r\n}\r\n@Override\r\nprotected void publishResults(CharSequence constraint, FilterResults results) {\r\nif (results != null &amp;&amp; results.count &gt; 0) {\r\nresultList = (List) results.values;\r\nnotifyDataSetChanged();\r\n} else {\r\nnotifyDataSetInvalidated();\r\n}}};\r\nreturn filter;\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>In WebServiceManager :<\/p>\n<pre class=\"lang:java decode:true\">private static Object requestServer(String urlStr) {\r\nHttpURLConnection httpURLConnection = null;\r\nObject response = null;\r\nInputStream inputStream;\r\nint ch;\r\ntry{\r\nURL url = new URL(urlStr);\r\nhttpURLConnection = (HttpURLConnection) url.openConnection();\r\nhttpURLConnection.setConnectTimeout( 3000 * 10000); \/\/Timeout until a connection is established\r\nhttpURLConnection.setReadTimeout(3000 * 10000); \/\/Timeout for waiting for data to come\r\nhttpURLConnection.setRequestMethod(\"POST\");\r\nhttpURLConnection.setRequestProperty(\"Content-Type\", \"application\/json\");\r\nhttpURLConnection.setDoInput(true);\r\nhttpURLConnection.connect();\r\ninputStream = new BufferedInputStream( httpURLConnection.getInputStream());\r\nStringBuffer stringBuffer = new StringBuffer();\r\nwhile ((ch = inputStream.read()) != -1) {\r\nstringBuffer.append((char) ch);\r\n}\r\nresponse = stringBuffer.toString();\r\n}\r\ncatch (Exception exception) {\r\nexception.printStackTrace();\r\n}\r\nfinally {\r\nhttpURLConnection.disconnect();\r\n}\r\nreturn response;\r\n}<\/pre>\n<p><span style=\"font-size: 1rem;\">Being synchronous call, call is made to server and the results are obtained before performFiltering() function stops executing. So the autocomplete list is obtained.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In order to implement synchronous server calling within filter, autocomplete textview is being used. Web service is hit with each character typed in Autocomplete list. For having data dynamically in the list, we can make customized adapter. I have implemented it by making this adapter to implement filterable interface. For getting data from server, two [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":4140,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[71],"tags":[],"class_list":["post-4136","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mobile"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>AutoCompleteView - Synchronous server call with Filter - InnovationM - Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.innovationm.com\/blog\/synchronous-server-calling-within-filter\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"AutoCompleteView - Synchronous server call with Filter - InnovationM - Blog\" \/>\n<meta property=\"og:description\" content=\"In order to implement synchronous server calling within filter, autocomplete textview is being used. Web service is hit with each character typed in Autocomplete list. For having data dynamically in the list, we can make customized adapter. I have implemented it by making this adapter to implement filterable interface. For getting data from server, two [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.innovationm.com\/blog\/synchronous-server-calling-within-filter\/\" \/>\n<meta property=\"og:site_name\" content=\"InnovationM - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2018-02-26T11:05:36+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-01-20T13:25:47+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/02\/synchronous-server-call-within-filter.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1147\" \/>\n\t<meta property=\"og:image:height\" content=\"637\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"InnovationM Admin\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"InnovationM Admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/synchronous-server-calling-within-filter\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/synchronous-server-calling-within-filter\\\/\"},\"author\":{\"name\":\"InnovationM Admin\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#\\\/schema\\\/person\\\/a831bf4602d69d1fa452e3de0c8862ed\"},\"headline\":\"AutoCompleteView &#8211; Synchronous server call with Filter\",\"datePublished\":\"2018-02-26T11:05:36+00:00\",\"dateModified\":\"2023-01-20T13:25:47+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/synchronous-server-calling-within-filter\\\/\"},\"wordCount\":163,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/synchronous-server-calling-within-filter\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2018\\\/02\\\/synchronous-server-call-within-filter.jpg\",\"articleSection\":[\"Mobile\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/synchronous-server-calling-within-filter\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/synchronous-server-calling-within-filter\\\/\",\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/synchronous-server-calling-within-filter\\\/\",\"name\":\"AutoCompleteView - Synchronous server call with Filter - InnovationM - Blog\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/synchronous-server-calling-within-filter\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/synchronous-server-calling-within-filter\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2018\\\/02\\\/synchronous-server-call-within-filter.jpg\",\"datePublished\":\"2018-02-26T11:05:36+00:00\",\"dateModified\":\"2023-01-20T13:25:47+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#\\\/schema\\\/person\\\/a831bf4602d69d1fa452e3de0c8862ed\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/synchronous-server-calling-within-filter\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/synchronous-server-calling-within-filter\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/synchronous-server-calling-within-filter\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2018\\\/02\\\/synchronous-server-call-within-filter.jpg\",\"contentUrl\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2018\\\/02\\\/synchronous-server-call-within-filter.jpg\",\"width\":1147,\"height\":637},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/synchronous-server-calling-within-filter\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"AutoCompleteView &#8211; Synchronous server call with Filter\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/\",\"name\":\"InnovationM - Blog\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#\\\/schema\\\/person\\\/a831bf4602d69d1fa452e3de0c8862ed\",\"name\":\"InnovationM Admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g\",\"caption\":\"InnovationM Admin\"},\"sameAs\":[\"http:\\\/\\\/www.innovationm.com\\\/\"],\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/author\\\/innovationmadmin\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"AutoCompleteView - Synchronous server call with Filter - InnovationM - Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.innovationm.com\/blog\/synchronous-server-calling-within-filter\/","og_locale":"en_US","og_type":"article","og_title":"AutoCompleteView - Synchronous server call with Filter - InnovationM - Blog","og_description":"In order to implement synchronous server calling within filter, autocomplete textview is being used. Web service is hit with each character typed in Autocomplete list. For having data dynamically in the list, we can make customized adapter. I have implemented it by making this adapter to implement filterable interface. For getting data from server, two [&hellip;]","og_url":"https:\/\/www.innovationm.com\/blog\/synchronous-server-calling-within-filter\/","og_site_name":"InnovationM - Blog","article_published_time":"2018-02-26T11:05:36+00:00","article_modified_time":"2023-01-20T13:25:47+00:00","og_image":[{"width":1147,"height":637,"url":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/02\/synchronous-server-call-within-filter.jpg","type":"image\/jpeg"}],"author":"InnovationM Admin","twitter_misc":{"Written by":"InnovationM Admin","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.innovationm.com\/blog\/synchronous-server-calling-within-filter\/#article","isPartOf":{"@id":"https:\/\/www.innovationm.com\/blog\/synchronous-server-calling-within-filter\/"},"author":{"name":"InnovationM Admin","@id":"https:\/\/www.innovationm.com\/blog\/#\/schema\/person\/a831bf4602d69d1fa452e3de0c8862ed"},"headline":"AutoCompleteView &#8211; Synchronous server call with Filter","datePublished":"2018-02-26T11:05:36+00:00","dateModified":"2023-01-20T13:25:47+00:00","mainEntityOfPage":{"@id":"https:\/\/www.innovationm.com\/blog\/synchronous-server-calling-within-filter\/"},"wordCount":163,"commentCount":0,"image":{"@id":"https:\/\/www.innovationm.com\/blog\/synchronous-server-calling-within-filter\/#primaryimage"},"thumbnailUrl":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/02\/synchronous-server-call-within-filter.jpg","articleSection":["Mobile"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.innovationm.com\/blog\/synchronous-server-calling-within-filter\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.innovationm.com\/blog\/synchronous-server-calling-within-filter\/","url":"https:\/\/www.innovationm.com\/blog\/synchronous-server-calling-within-filter\/","name":"AutoCompleteView - Synchronous server call with Filter - InnovationM - Blog","isPartOf":{"@id":"https:\/\/www.innovationm.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.innovationm.com\/blog\/synchronous-server-calling-within-filter\/#primaryimage"},"image":{"@id":"https:\/\/www.innovationm.com\/blog\/synchronous-server-calling-within-filter\/#primaryimage"},"thumbnailUrl":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/02\/synchronous-server-call-within-filter.jpg","datePublished":"2018-02-26T11:05:36+00:00","dateModified":"2023-01-20T13:25:47+00:00","author":{"@id":"https:\/\/www.innovationm.com\/blog\/#\/schema\/person\/a831bf4602d69d1fa452e3de0c8862ed"},"breadcrumb":{"@id":"https:\/\/www.innovationm.com\/blog\/synchronous-server-calling-within-filter\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.innovationm.com\/blog\/synchronous-server-calling-within-filter\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.innovationm.com\/blog\/synchronous-server-calling-within-filter\/#primaryimage","url":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/02\/synchronous-server-call-within-filter.jpg","contentUrl":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/02\/synchronous-server-call-within-filter.jpg","width":1147,"height":637},{"@type":"BreadcrumbList","@id":"https:\/\/www.innovationm.com\/blog\/synchronous-server-calling-within-filter\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.innovationm.com\/blog\/"},{"@type":"ListItem","position":2,"name":"AutoCompleteView &#8211; Synchronous server call with Filter"}]},{"@type":"WebSite","@id":"https:\/\/www.innovationm.com\/blog\/#website","url":"https:\/\/www.innovationm.com\/blog\/","name":"InnovationM - Blog","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.innovationm.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.innovationm.com\/blog\/#\/schema\/person\/a831bf4602d69d1fa452e3de0c8862ed","name":"InnovationM Admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g","caption":"InnovationM Admin"},"sameAs":["http:\/\/www.innovationm.com\/"],"url":"https:\/\/www.innovationm.com\/blog\/author\/innovationmadmin\/"}]}},"_links":{"self":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/posts\/4136","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/comments?post=4136"}],"version-history":[{"count":0,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/posts\/4136\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/media\/4140"}],"wp:attachment":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/media?parent=4136"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/categories?post=4136"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/tags?post=4136"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}