java - Prioritizing Network call in android using AsyncTask -


i looking prioritize network calls in android application in order download content in order of priority.

problem: making app downloads feeds (involves images , text) , comments (only on requests , contain texts, images has been download already). on displaying feed send request image of feed takes time download. meanwhile user can request comment corresponding feed. current implementation waits until image gets downloaded sends request comment make user wait long time.

current solution: have used singleton class send network call , factory design service classes.

downloading feed: have used viewholder displaying feeds (which contains images) code downloading image (manually)

if(ioutil.fileexists(itemview.getcontext(), feed.getproductimagename())) {         if(application.ismemoryavailableinheap()) {             int height= constant.feed_image_height;             log.v(constant.tag,"feed:"+feed.getname()+" has height:"+height+" not resizing");             int width = screenutility.getscreensize((activity) itemview.getcontext()).getwidth();             log.v(constant.tag, "width="+width);             linearlayout.layoutparams layoutparamforimgproduct = new linearlayout.layoutparams(width, height);             imgproducts.setlayoutparams(layoutparamforimgproduct);             picasso.with(this.itemview.getcontext()).load(imageutil.getlocalimageuri(itemview.getcontext(), feed.getproductimagename())).resize(width,height).centercrop().into(this.imgproducts);         }         else         {             system.gc();         }     }     else {         log.v(constant.tag,"downloading productscreen image");         webrequest request = new webrequest();         request.seturl(constant.root_application_url_with_separator + feed.getimgpath());         request.setparam(feed.getproductimagename() + "");         session.save(feed.getproductimagename()+"",imgproducts);         new imagedownloadtask(this, true, feed.getproductimagename(), itemview.getcontext()).execute(request);     } 

call imagedownloadtask asynctask which, after downloading image, calls complete method of view holder class

complete method

 public void complete(object o) {     if(o instanceof imagedetails) {         imagedetails id =(imagedetails) o;         if(id.getlocalname() != null) {             string key = id.getid();             imageview imgview = (imageview) pixyfisession.get(key);             if(!id.getlocalname().contains(".png")) {                 if(feed.getproductimagename().equals(id.getlocalname())) {                     log.v(constant.tag, " feed name:" + feed.getproductimagename() + " local name:" + id.getlocalname());                     int height = imageutil.scaleimageheighttofitwidth(id.getlocalname(), itemview.getcontext());                     log.v(constant.tag, "feed:" + feed.getname() + " has height:" + height);                     linearlayout.layoutparams layoutparamforimgproduct = new linearlayout.layoutparams(linearlayout.layoutparams.match_parent, height);                     imgview.setlayoutparams(layoutparamforimgproduct);                     picasso.with(itemview.getcontext()).load(imageutil.getlocalimageuri(itemview.getcontext(), id.getlocalname())).into(imgview);                 }             }             else {                 log.v(constant.tag,"this should profile picture:"+"circle_"+id.getlocalname());                 picasso.with(itemview.getcontext()).load(imageutil.getlocalimageuri(itemview.getcontext(), "circle_"+id.getlocalname()+".png")).fit().centercrop().into(imgview);             }         }     } } 

my asynctask sets downloaded image's detail in imagedetail class , use picasso set image

i use similar process download feed's comment well. network call goes through single class's static method

class:

public class webutil {    public static string makepostrequest(string url,string params) {     httpurlconnection connection = null;     string response = null;     context context = (context) pixyfisession.get(constant.global_context);     if(application.isnetworkconnected(context)) {         log.v(constant.tag, "sending post request to:" + url);         try {             string token = pixyfisession.get(constant.auth_token).tostring();             params="token="+token+"&"+params;             user loggedinuser = (user) pixyfisession.get(constant.logged_in_user);             if(loggedinuser != null)                 params="loggedinuser="+loggedinuser.getuserid()+"&"+params;             //log.v(constant.tag,"param:"+params);             url resturl = new url(url);             connection = (httpurlconnection) resturl.openconnection();             connection.setconnecttimeout(60000);             connection.setrequestmethod("post");             connection.setdooutput(true);             outputstream os = connection.getoutputstream();             outputstreamwriter writer = new outputstreamwriter(os, "utf-8");             writer.write(params);             writer.flush();             inputstream = new bufferedinputstream(connection.getinputstream());             response = convertstreamtostring(is);         } catch (malformedurlexception e) {             log.e(constant.tag,"malformed url:"+url);             log.e(constant.tag, e.getmessage(), e);         } catch (ioexception e) {             if(e instanceof eofexception) {                 if(postrequest<4) {                     connection.disconnect();                     postrequest++;                     return makepostrequest(url,params);                 }             }             log.e(constant.tag,"io exception param:"+params);             log.e(constant.tag, e.getmessage(), e);         }          log.v(constant.tag, "response returned");         log.v(constant.tag, "response:" + response);     }     postrequest = 0;     return response;     }   } 

i not sure whether comment's request blocks due locking issue. both pf request take place using different thread, highly unlikely.

another approach: removed manual call image , used picasso downloading images well. not sure whether picasso makes web call each time image needs shown or maintain cache (the reason why downloading images manually).

requirement please let me know solution through download feed's comment while downloading image


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -