package com.eyekabob;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class FindMusicActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.findmusicactivity);
    }

    public void findByArtistHandler(View v) {
        EditText findByArtist = (EditText)findViewById(R.id.findByArtistInput);
        String artist = findByArtist.getText().toString();
        URL url = null;
		try {
			url = new URL(EyekabobHelper.LastFM.SERVICE_URL + "?method=artist.getEvents&api_key=" + EyekabobHelper.LastFM.API_KEY + "&artist=" + artist);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}

        HttpURLConnection urlConnection = null;
		try {
			urlConnection = (HttpURLConnection) url.openConnection();
            InputStream in = new BufferedInputStream(urlConnection.getInputStream());
            String result = getStringResponse(in);
            Toast.makeText(v.getContext(), result, 8000).show();
		} catch (IOException e) {
			e.printStackTrace();
		}
        finally {
            urlConnection.disconnect();
        }
    }

    public String getStringResponse(InputStream is) throws IOException {
        StringBuffer out = new StringBuffer();
        byte[] b = new byte[4096];
        for (int n; (n = is.read(b)) != -1;) {
            out.append(new String(b, 0, n));
        }
        return out.toString();
    }
}

@@ -233,16 +233,45 @@
 tivity;%0A
+import android.os.AsyncTask;%0A
 import a
@@ -1030,274 +1030,267 @@
 %09%7D%0A%0A
-        HttpURLConnection urlConnection = null;%0A%09%09try %7B%0A%09%09%09urlConnection = (HttpURLConnection) url.openConnection(
+%09%09new ArtistTask().execute(url);%0A%0A    %7D%0A%0A    private class ArtistTask extends AsyncTask%3CURL, Void, String%3E %7B%0A    %09protected String doInBackground(URL... urls) %7B%0A    %09%09return doRequest(urls%5B0%5D
 );%0A    
- 
-       InputStream in = new BufferedInputStream(urlConnection.getInputStream());%0A            String result = getStringResponse(in);%0A            
+%09%7D%0A%0A    %09protected void onPostExecute(String result) %7B%0A    %09%09
 Toas
@@ -1304,13 +1304,22 @@
 ext(
-v.
 get
+Application
 Cont
@@ -1337,9 +1337,10 @@
 lt, 
-8
+50
 000)
@@ -1352,94 +1352,160 @@
 ();%0A
-%09%09%7D catch (IOException e) %7B%0A%09%09%09e.printStackTrace();%0A%09%09%7D%0A        finally %7B%0A        
+    %09%7D%0A        public String doRequest(URL url) %7B%0A        %09HttpURLConnection urlConnection = null;%0A        %09StringBuffer out = null;%0A    %09%09try %7B%0A
     
+%09%09%09
 urlC
@@ -1517,19 +1517,49 @@
 tion
-.disc
+ = (HttpURLConnection) url.openC
 onnect
+ion
 ();%0A
@@ -1570,110 +1570,105 @@
     
-%7D%0A
     
-%7D%0A%0A
     
-public String getStringResponse(InputStream is) throws IOException %7B%0A        StringBuffer
+InputStream is = new BufferedInputStream(urlConnection.getInputStream());%0A               
  out
@@ -1686,24 +1686,32 @@
 ngBuffer();%0A
+        
         byte
@@ -1741,16 +1741,24 @@
         
+     
+   
 for (int
@@ -1800,16 +1800,24 @@
         
+        
 out.appe
@@ -1849,18 +1849,179 @@
         
+  
-%7D%0A
+      %7D%0A    %09%09%7D catch (IOException e) %7B%0A    %09%09%09e.printStackTrace();%0A    %09%09%7D%0A            finally %7B%0A                urlConnection.disconnect();%0A            %7D%0A%0A    
         
@@ -2039,20 +2039,30 @@
 toString();%0A
+        %7D%0A
     %7D%0A%7D%0A

use another thread for the artist task