java - AsyncTasck and Thread -


i'm confused working asynctask , thread.i know there no need create new thread in asynctask, have to.in asynctask data server , need photo server. create request social network's db , it. request executes in new thread. here problem: asynctask execute half of requests , of them. 11 requests , executes 7 of them

the acyntask:

  public db_read_all(context _context){     context = _context; } @override protected string doinbackground(string... strings) {     list<namevaluepair> params = new arraylist<namevaluepair>();      // getting json string url     jsonobject json = jparser.makehttprequest(url_read_all, "get", params);      // check log cat json reponse     if(json!=null){         //log.e("users_all: ", json.tostring());         try {             // checking success tag             int success = json.getint(tag_success);             if (success == 1) {                 users = json.getjsonarray(tag_users);                 log.e("succes getting users, users amount",                 string.valueof(users.length()));                  (int = 0; < users.length(); i++) {                      jsonobject c = users.getjsonobject(i);                     id = c.getstring(tag_id);                     first_name = c.getstring(tag_first_name);                     listoffname.add(first_name);                      last_name = c.getstring(tag_last_name);                       listoflname.add(last_name);                      vk_id = c.getstring(tag_vk_id);                     listofvkid.add(vk_id);                      points= c.getstring(tag_points);                     listofpoints.add(points);                 }             } else {                   log.e("db_read_all","db error!");             }         } catch (jsonexception | unsupportedencodingexception e) {             e.printstacktrace();         }         catch (exception e){              logger logger = logger.getanonymouslogger();             logger.log(level.severe, "an exception thrown while converting", e);             log.e("db_read_all","db error!");         }     } else{         log.e("db_read_all","json error");     }      for(int = 0; i<listoffname.size();i++){         setuserphotourl(listoffname.get(i), listoflname.get(i), listofvkid.get(i), listofpoints.get(i));     }     return null; } 

method request in new thread:

private void setuserphotourl(final string _first_name,                              final string _last_name,final string _vk_id,final string _points){     vkrequest request = vkapi.users().get(vkparameters.from(vkapiconst.user_id, _vk_id,             vkapiconst.fields, "photo_100"));      request.executewithlistener(new vkrequest.vkrequestlistener() {         @override         public void oncomplete(final vkresponse response) {             super.oncomplete(response);             //log.e("second", string.valueof(i));             //log.e("second", string.valueof(_first_name));                     vklist<vkapiuser> user = (vklist<vkapiuser>) response.parsedmodel;                     if(user.get(0).photo_100!=null)                     photo_url = user.get(0).photo_100;                      bitmap photobm = null;                      sharedpreferences prefs = preferencemanager.getdefaultsharedpreferences(context);                     sharedpreferences.editor editor = prefs.edit();                     if(photo_url !=null){                         try {                             photobm = internet.converturltoimage(photo_url);                         }                         catch (exception e)                         {                             logger logger = logger.getanonymouslogger();                             logger.log(level.severe, "an exception thrown while converting", e);                         }                     }                     bytearrayoutputstream baos = new bytearrayoutputstream();                     if(photobm!= null)                         photobm.compress(bitmap.compressformat.png, 100, baos);                      byte[] b = baos.tobytearray();                     string encodedphoto = base64.encodetostring(b, base64.default);              editor.putstring("userfirstname" + string.valueof(i), _first_name);             editor.putstring("userlastname" + string.valueof(i), _last_name);             editor.putstring("uservkid" + string.valueof(i), _vk_id);             editor.putstring("userpoints" + string.valueof(i), _points);             editor.putstring("userphoto"+string.valueof(i),encodedphoto);             editor.apply();              if(i == users.length() - 1){                 log.e("db_read_all","success");             }             log.e("second",string.valueof(i));             i++;         } 

thread/runable runs in separate thread other main gui thread, while async running on both main , worker thread, onpreexecute/onpostexecute runs in main thread while doinbackground runs in separate thread says worker thread, while problem not create thread inside doinbackground because thread, why creating thread load/post data from/to server, can as u can in doinbackground, yes if want execute parallel execution use threadpool executor start asynctask. yes can simple threads runs on separate thread can not directly control these threads if not using handlers etc. highly suggest use async tasks on threads/runnable


Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -