java - AsyncTask freezing both its thread and the UI thrad -


i've made class extending asynctask send post requests server, application. has been working until now. haven't changed anything, of sudden freezing ui thread , own thread when try execute it.

i've tried using log.d() find out gets u before freezing, , turns out freezes right after doinbackground() called.

i've tried changing this...

string result = new asyncpost().execute(params).get();

to this...

asynctask task = new asyncpost(); task.execute(); string result = task.get(); 

it nothing parameters i'm giving it, since freezes no matter parameters passed it.

i have tried using postman check server working (and is), not server-side issue either.

this asynctask class. sorry excessve amount of code, can't figure out specific part of bad, since working until now.

public class asyncpost extends asynctask<object, integer, string> {     @override     protected string doinbackground(object ... params){         //android.os.process.setthreadpriority(process.thread_priority_more_favorable);          object urlobj = params[0];         object argsobj = params[1];          string urlstr = (string) urlobj;         map<string, string> args = (map<string, string>) argsobj;          url url;         httpsurlconnection http = null;          byte[] out = null;         int len = 0;         log.d("post", "started");          try {             url = new url(urlstr);              http = (httpsurlconnection) url.openconnection();             http.setrequestmethod("post");             http.setdooutput(true);              stringjoiner sj = new stringjoiner("&");              (map.entry<string, string> entry : args.entryset()) {                 sj.add(urlencoder.encode(entry.getkey(), "utf-8") + "="                         + urlencoder.encode(entry.getvalue(), "utf-8"));             }              out = sj.tostring().getbytes(standardcharsets.utf_8);             len = out.length;              http.setfixedlengthstreamingmode(len);             http.setrequestproperty("content-type", "application/x-www-form-urlencoded; charset=utf-8");              http.connect();             log.d("post", "connected");         }          catch (exception e){             e.printstacktrace();             return null;         }          try (outputstream os = http.getoutputstream()){             os.write(out);         }          catch (exception e){             e.printstacktrace();             return null;         }          try {             log.d("post_responsecode", integer.tostring(http.getresponsecode()));             bufferedreader responsereader = new bufferedreader(new inputstreamreader(http.getinputstream()));             stringbuilder responsebuilder = new stringbuilder();             string response;              while ((response = responsereader.readline()) != null){                 responsebuilder.append(response);             }              log.d("post", responsebuilder.tostring());              return responsebuilder.tostring();         }          catch (exception e){             e.printstacktrace();             return null;         }          finally{             http.disconnect();         }     }      @override     public void onprogressupdate(integer ... progress){         log.d("post", "progress = " + integer.tostring(progress[0]));     } } 

what should fix problem?

should using asynctask @ task. i've tried using thread, freezes ui too.

i've looked @ other questions, seem doing this...

object result = new asynctask().get();

which causing problems, doesn't apply me.

if don't want wait result on main thread, need use interface used asyncpost class in overriden onpostexecute method. don't forget make receiving class implement it:

asyncpostlistener

interface asyncpostlistener {     void continuewith(string result); } 

mainactivity

@override public void continuewith(string results) {     //continue logic here } 

asyncpost

 public class asyncpost extends asynctask<object, integer, string> {     private asyncpostlistener listener;     ....     public asyncpost(asyncpostlistener listener){         this.listener = listener;     }      @override     protected void onpostexecute(string s) {         super.onpostexecute(s);         if(listener != null)             listener.continuewith(s);     } } 

usage:

asyncpost ap = new asyncpost(this); 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -