Sunday 15 March 2015

android.os.NetworkOnMainThreadException in AsyncTask's doInBackground -



android.os.NetworkOnMainThreadException in AsyncTask's doInBackground -

why in asynctask should android.os.networkonmainthreadexception? thought asynctask solution problem. exxeption on line 7.

private class imagedownloadtask extends asynctask<string, integer, byte[]> { @override protected byte[] doinbackground(string... params) { seek { url url = new url(params[0]); urlconnection connection = url.openconnection(); inputstream inputstream = connection.getinputstream(); bytearrayoutputstream bytebuffer = new bytearrayoutputstream(); int buffersize = 1024; byte[] buffer = new byte[buffersize]; int len; while ((len = inputstream.read(buffer)) != -1) { bytebuffer.write(buffer, 0, len); } homecoming bytebuffer.tobytearray(); } grab (ioexception ex) { homecoming new byte[0]; } } }

i want utilize downloading picture.

public byte[] getprofilepicture(context context, string id) { string url = context.getstring(r.string.facebook_picture_url_large, id); imagedownloadtask task = new imagedownloadtask(); homecoming task.doinbackground(url); }

by calling doinbackground() directly, not using asynctask functionality. instead, should phone call execute() , utilize results overriding asynctask's onpostexecute() method explained in usage section of same page.

android android-asynctask networkonmainthread

No comments:

Post a Comment