Wednesday 15 April 2015

java - HttpAsyncTask returns null as data -



java - HttpAsyncTask returns null as data -

we have problem getting httpasynctask homecoming string our simple http function: want able send info url , receive proper response can utilize later in our other classes. httphandler class receive right response until point.

here our httphandler:

public class httprequester extends activity { static string result; public string createuser(string username) { new httpasynctask().execute("http://188.226.252.112/createplayer.php?name=" + username); system.out.println("id @ createuser is; " + result); // returns null homecoming result; } public static string get(string url){ inputstream inputstream = null; seek { // create httpclient httpclient httpclient = new defaulthttpclient(); // create request given url httpresponse httpresponse = httpclient.execute(new httpget(url)); // receive response inputstream inputstream = httpresponse.getentity().getcontent(); // convert inputstream string if(inputstream != null) result = convertinputstreamtostring(inputstream); else result = "did not work!"; } grab (exception e) { log.d("inputstream", e.getlocalizedmessage()); } system.out.println("id @ is; " + result); // returns right id homecoming result; } private static string convertinputstreamtostring(inputstream inputstream) throws ioexception{ stringbuilder sb = new stringbuilder(); string something; bufferedreader bufferedreader = new bufferedreader( new inputstreamreader(inputstream)); while ((something = bufferedreader.readline()) != null) { sb.append(something); } inputstream.close(); result = sb.tostring(); system.out.println("id @ convert is; " + result); // returns right id homecoming result; } public boolean isconnected(){ connectivitymanager connmgr = (connectivitymanager) getsystemservice(this.connectivity_service); networkinfo networkinfo = connmgr.getactivenetworkinfo(); if (networkinfo != null && networkinfo.isconnected()) homecoming true; else homecoming false; } private class httpasynctask extends asynctask<string, void, string> { @override protected string doinbackground(string... urls) { homecoming get(urls[0]); } }

}

and here other class' function has receive info http handler:

httprequester requester = new httprequester(); id = requester.createuser(login); system.out.println("id in other class is: " + id); // returns null

please help

regards

cong vinh

your approach incorrect. when phone call new httpasynctask().execute() starts new asynchronous operation (a new thread spawn under hood). execute method returns , result property stays null. why createuser method returns null. get method synchronous. in context of programme executed in separate thread (the thread started httpasynctask) methods in executed consistently. why result property here initialized , holds right result.

it not practice have shared property (the result property) between different threads. can root of serious concurrent problems. improve approach maintain property part of httpasynctask class , homecoming result of asynctask execution. do processing of result in postexecuted method of httpasynctask class (that not implemented here can overridden). right way can sure processing executed after http phone call has been made , result has been received.

java android android-asynctask httprequest

No comments:

Post a Comment