java – Android Development: How do I integrate threads in this?

function starhover(thisid) {
document.getElementById(thisid).src=”http://androidask.phpfogapp.com/wp-content/plugins/wp-reviewstars-premium/starhover.png”;
}
function starout(thisid) {
document.getElementById(thisid).src=”http://androidask.phpfogapp.com/wp-content/plugins/wp-reviewstars-premium/star.png”;
}
function starup() {
document.getElementById(‘stargroup’).innerHTML=’thank you for your vote!’;
}
function stardown(jthisid) {
document.getElementById(jthisid).src = “http://androidask.phpfogapp.com/wp-content/plugins/wp-reviewstars-premium/starclick.png”;
var jpostid=document.getElementById(jthisid).className;
$.post(“http://androidask.phpfogapp.com/wp-content/plugins/wp-reviewstars-premium/updatepostmeta.php”, { postid: jpostid, thisid : jthisid , rating: 1, count: 3 });
document.getElementById(‘stargroup’).innerHTML=’thank you for your vote!’;
}

So I\’m trying to make a youtube application by retrieving the json data from a youtube channel. Problem is I\’m great at writing single-threaded applications. But when it comes to multithreading I always lose my insight.

I haven\’t learned it in school yet and most tutorials are rubbish, or atleast I find that.

So what I want to do is introduce a thread to my \”GetVideos\” class so it doesn\’t slow down my application while retrieving the videos. I know I\’ll have to use a handler and thread but everytime I try to use them my application crashes. Can you guys help?

public class GetVideos { //class that retrieves JSON data based on youtube username

private String channelName;
private HttpClient client; //client that gets info
private VideoLibrary lib;


public GetVideos(String channelName) {
    this.channelName = channelName;
    client = new DefaultHttpClient();
    lib = new VideoLibrary();
    fillData();
}

private void fillData() {
    try {
        final String URL = \"http://gdata.youtube.com/feeds/api/users/\" + channelName + \"/uploads?v=2&alt=jsonc\";
        HttpGet get = new HttpGet(URL);
        HttpResponse response = client.execute(get);
        String jsonString = EntityUtils.toString(response.getEntity());
        JSONObject json = new JSONObject(jsonString);
        JSONArray jsonArray = json.getJSONObject(\"data\").getJSONArray(\"items\");
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject items = jsonArray.getJSONObject(i);
            String title = items.getString(\"title\");
            String thumbUrl = items.getJSONObject(\"thumbnail\").getString(\"sqDefault\");
            String url = items.getJSONObject(\"player\").getString(\"default\");
            lib.addVideo(new Video(title, url, thumbUrl));

        }

    } catch (JSONException e) {
        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    } catch (ClientProtocolException e) {
        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    } catch (IOException e) {
        e.printStackTrace();  
       }
   }

    public VideoLibrary getLib() {
         return lib;
    } 
 }

public class SxePhil extends Activity { //class that makes up the interactive end of
//the app, here the JSON data is put in a list 
private ListView list;
private GetVideos g;
private VideoLibrary videoLibrary;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sxephil);
    list = (ListView) findViewById(R.id.sxephilList);
    g = new GetVideos(\"sxephil\");
    videoLibrary = g.getLib();

    ArrayList<String> titles = new ArrayList<String>();
    for (int i = 0; i < 25; i++) {
        titles.add(videoLibrary.getVideos().get(i).getTitle());
    }


       ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.simplerow, titles);
        list.setAdapter(adapter);
   }  
}

Basically what I tried was implementing the threading that is used here: http://blog.blundell-apps.com/show-youtube-user-videos-in-a-listview/

in my code, because this project was built on an older sdk, so I wanted to modernize it so to speak. Look at the GetYoutube*** class and MainActivity class that\’s where the money is

Rating: 1 out of 5 based on 3 ratings

Posts Related to java – Android Development: How do I integrate threads in this?

The post java – Android Development: How do I integrate threads in this? appeared first on Android ASK.

via Android ASK http://androidask.phpfogapp.com/java-android-development-how-do-i-integrate-threads-in-this.html

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: