App Freezing when getting data from the internet

I'm having a problem that i'm sure that it's really simple to solve but i'm still starting on app developing so i thought you could help me out. I got an app that has a currency converter in it, and when i push the button to get the conversion it freezes while it's getting the data from the internet and then comes back to life after some seconds. Here is the code:

   public void capturaConversao(String m1, String m2) throws ClientProtocolException, IOException {
        BufferedReader resp = null;
        endereco = "http://download.finance.yahoo.com/d/quotes.csv?s=" + m1
                + m2 + "=X&f=sl1d1t1ba&e=.csv";
        try {
            HttpClient client = new DefaultHttpClient();
            HttpGet method = new HttpGet(endereco);
            HttpResponse statusCode = client.execute(method);
            resp = new BufferedReader(new InputStreamReader(statusCode
                    .getEntity().getContent()));
            StringBuffer sb = new StringBuffer("");
            String line = "";
            while ((line = resp.readLine()) != null) {
                sb.append(line);
            }
            resp.close();
            String result = sb.toString();
            String[] values = result.split(",");
            conversion = values[1];
            nextstep++;

        } finally {
            if (resp != null) {
                try {
                    resp.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    Toast.makeText(getBaseContext(), "Não foi possível se conectar à internet.", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }

Answers


AsyncTask is the solution for almost all the UI thread woes. :) Here's a short tutorial.


You are likely calling this from the UI thread, which you should not do. It makes the UI lock up, because the UI thread is busy connecting to the internet. Here's a tutorial for making such a call.


Your UI thread's available cycles are being hogged by the HTTP actions. Seperate it out with either a Thread or an AsyncTask


Yes i am also agree to go with AsyncTask solution and as it is known as Painless Threading, user need to not to bother about Thread Management.

For your case, just call the capturaConversao() method inside doInBackground() (i.e. do all data fetching operation from web inside the doInBackground method) and call the display method inside the onPostExecute() (i.e. do all display operation like display fetched values inside the listview or textview).


Need Your Help

Problem with jQuery slider - time

php jquery time slider

I'd like to make a slider to choose time(from 1:00 to 4:00) but it doesn't work. It doesn't communicate with 'select'. Can anyone help me?

Calling functions from a c++ DLL in Delphi

c++ delphi dll delphi-7

I created a new c++ DLL project in VS2010 that exposes 1 function

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.