Issue
I have an issue, which is not that big, but to the user it is bad. The app basically gets the user’s input of some place and, when the user clicks on the button, a URL to the Google API with the place on its parameter is sent to an AsyncTask, where it sends this URL via HttpGet and is returned a JSONArray with everything needed. The problem is, when I click on the button and the internet is not that good, the button seems to “freeze” like this:
My activity code is below:
public class MainActivity extends Activity{
...
protected void onCreate(Bundle savedInstanceState){...}
public void onResume()}
btnSearch.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
String search txtSearch.getText().toString();
try{
List<Location> locations new SearchTask(MainActivity.this).execute(strSearch).get();
if(locations ! null){
ArrayAdapter<Location> adapter new ArrayAdapter<Location>(MainActivity.this, android.R.layout.simple_list_item_1, locations);
listView.setAdapter(adapter);
...
}
}
}
}
}
}
My AsyncTask class code is below:
public class SearchTask extends AsyncTask<String, Void, List<Location>>{
...
protected List<Location> doInBackground(String... params){
if(isNetworkAvailable()){
HttpGet httpGet null;
HttpClient client null;
HttpResponse response null;
StringBuilder builder null;
try{
String param URLDecoder.decode(params[0], "UTF-8").replace(" ", "%20");
httpGet new HttpGet("http://maps.googleapis.com/maps/api/geocode/json?address" + param + "&sensorfalse");
client new DefaultHttpClient();
builder new StringBuilder();
}
catch(UnsupportedEncodingException e){
Log.i("Error", e.getMessage());
}
try{
response client.execute(httpGet);
HttpEntity entity response.getEntity();
InputStream stream entity.getContent();
BufferedReader br new BufferedReader(new InputStreamReader(stream, "UTF-8"));
int val;
while((val br.read()) ! -1){
builder.append((char) val);
}
}
catch(IOException e){
Log.i("Error", e.getMessage());
}
JSONObject jsonObject new JSONObject();
List<Location> listLocation new ArrayList<Location>();
int countJson 0;
try{
jsonObject new JSONObject(builder.toString());
JSONArray jArray jsonObject.getJSONArray("results");
countJson jArray.length();
for(int i 0; i < countJson; i++){
Location location new Location();
String formattedAddress ((JSONArray) jsonObject.get("results")).getJSONObject(i).getString("formatted_address");
double lat ((JSONArray) jsonObject.get("results")).getJSONObject(i).getJSONObject("geometry").getJSONObject("location").getDouble("lat");
double lng ((JSONArray) jsonObject.get("results")).getJSONObject(i).getJSONObject("geometry").getJSONObject("location").getDouble("lng");
location.setFormattedAddress(formattedAddress);
location.setLat(lat);
location.setLng(lng);
listLocation.add(location);
}
}
catch(JSONException e){
Log.i("Error", e.getMessage());
}
return listLocation;
}
else{
return null;
}
}
@Override
protected void onPreExecute(){
super.onPreExecute();
progress new ProgressDialog(context);
progress.setMessage("Loading...");
progress.show();
}
@Override
protected void onPostExecute(List<Location> result){
super.onPostExecute();
progress.dismiss();
}
private boolean isNetworkAvailable(){
ConnectivityManager connManager (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info connManager.getActiveNetworkInfo();
return info ! null && info.isConnected();
}
}
The ListView is on the same xml of the EditView and the Button.
Is there a way to improve it in order to make the UI not behave like this?
Thanks!
[by Taboola](https://popup.taboola.com/en/?templatecolorbox&utmsourceangularfix&utmmediumreferral&utmcontentthumbnails-mid:Mid Article Thumbnails:)[by Taboola](https://popup.taboola.com/en/?templatecolorbox&utmsourceangularfix&utmmediumreferral&utmcontentthumbnails-mid:Mid Article Thumbnails:)
[Sponsored Links](https://popup.taboola.com/en/?templatecolorbox&utmsourceangularfix&utmmediumreferral&utmcontentthumbnails-mid:Mid Article Thumbnails:)[Sponsored Links](https://popup.taboola.com/en/?templatecolorbox&utmsourceangularfix&utmmediumreferral&utmcontentthumbnails-mid:Mid Article Thumbnails:)
[Promoted Links](https://popup.taboola.com/en/?templatecolorbox&utmsourceangularfix&utmmediumreferral&utmcontentthumbnails-mid:Mid Article Thumbnails:)[Promoted Links](https://popup.taboola.com/en/?templatecolorbox&utmsourceangularfix&utmmediumreferral&utmcontentthumbnails-mid:Mid Article Thumbnails:)
You May Like
[
CNA
](https://www.channelnewsasia.com/singapore/tradenation-luxury-goods-scam-timeline-couple-pi-jiapeng-siriwipa-pansuk-police-interpol-2841536?cidtaboolaidpaid25042022cnamkt-news_angularfix&tblciGiCyu-3eetpfqXFavMqYA8AXMjOsh5NCt0UvXq7Kt4cjkSDsklIorunqm8XKtsnGAQ#tblciGiCyu-3eetpfqXFavMqYA8AXMjOsh5NCt0UvXq7Kt4cjkSDsklIorunqm8XKtsnGAQ “Timeline: How a couple fled Singapore after millions were lost in Tradenation luxury goods scam”)Timeline: How a couple fled Singapore after millions were lost in Tradenation luxury goods scamCNA
[
Rich Houses
](https://rfvtgb.richouses.com/worldwide/lifesa-ta?utmmediumtaboola&utmsourcetaboola&utmcampaignto-rh-lifesa1-mac-0w-ct-27082d&utmtermangularfix&utm_bidSMpg3ju9BSX0xGVu4WHaGl7p299Zjy0TPOeOsRpkLq8&utmkGiCyu-3eetpfqXFavMqYA8AXMjOsh5NCt0UvXq7Kt4cjkSDu0VUogv3Y8sGIl9Ju “Always Place a Plastic Bottle on Your Tires, Here’s Why”)Always Place a Plastic Bottle on Your Tires, Here’s WhyRich Houses
[
Online Bank Acc
](https://yrebr.com/cid/832e9ee7-ae85-490f-a8b9-32efae006424?campaignid20884743&platformDesktop&campaignitemid3563516753&siteangularfix&clickidGiCyu-3eetpfqXFavMqYA8AXMjOsh5NCt0UvXq7Kt4cjkSCMyVQohfO0w8Xt1q6LAQ×tamp2022-09-27+08%3A03%3A44&thumbnailhttps%3A%2F%2Fyrebr.com%2Fcontent%2Ff17962ac-50bd-47c7-8eaf-50f61e6eacec&titleLooking+For+a+Feeless+Bankaccount%3F+Discover+Here#tblciGiCyu-3eetpfqXFavMqYA8AXMjOsh5NCt0UvXq7Kt4cjkSCMyVQohfO0w8Xt1q6LAQ “Looking For a Feeless Bankaccount? Discover Here”)Looking For a Feeless Bankaccount? Discover HereOnline Bank AccSearch Now
Solution
Try this:
btnSearch.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
String search txtSearch.getText().toString();
new SearchTask(MainActivity.this).execute(strSearch);
}
}
@Override
protected void onPostExecute(List<Location> locations){
if(locations ! null){
ArrayAdapter<Location> adapter new ArrayAdapter<Location>(MainActivity.this, android.R.layout.simple_list_item_1, locations);
listView.setAdapter(adapter);
}
progress.dismiss();
}
Answered By – Misagh Emamverdi