0

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:

button freezed

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 + "&sensor=false");
                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!

1 Answer 1

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.