java - HttpAsyncTask returns null as data -


To return a string from our simple http function, we have a problem in getting the HTTPPiccon Task: just send us the data Want to be able to get a URL and receive a reasonable response which we can use later in our other sections. One areafindler class gets the correct response to a point.

Here is our subsidiary funder:

  the expansion of the public square HttpRequester activity (Strict string result; public string make the user (string user name) {execute new HTTPPicink Task (). ("http://188.226.252.112/createPlayer.php?name=" + username); System.out.println ("user ID id; result is;); // also returns null return result;} Public static string received (string url) {InputStream inputStream = null; Tr Y {// HttpClient HTTP Client httpclient = Create New DefaultHttpClient (); // Request the URL HttpResponse httpResponse = httpclient.execute (new HttpGet (url)); // inputstream inputStream = httpResponse.getEntity () Convert GetContent (); // InputStream to String if (inputstream! = Null) result = Convert InputStreamToothString (inputstream); and the result = "Not done!";} Hold (Exception e) {Log D. ("Inputstream", E.A.P.A.Olyclised Message ());} System.out.pri Ntln ("get on ID;" result); // This gives correct ID return results; } Private Stable String throws Convert InputStreamToRostring (InputStream Inputstream) IOException {StringBinder S.B. = New stringbilder (); String something; Buffer Redder BuffettRedder = New Buffettder (new inputstreamer (inputstream)); While ((some = bufferedReader.readLine ())! = Tap) {sb.append (some); } Inputstream.close (); Result = sb.toString (); System.out.println ("Conversion is ID;" +); // This gives correct ID return results; } Public Boolean Connect is () {Connectivity Manager connMgr = (Connectivity Manager) getSystemService (this.CONNECTIVITY_SERVICE); NetworkInfo NetworkInfo = connMgr.getActiveNetworkInfo (); If (networkInfo! = Null & amp; networkInfo.isConnected ()) return true; Second false return; } Private class HttpAsyncTask AsyncTask Extended & lt; String, zero, string & gt; {@Override protected string doInBackground (string ... url) {return (URL [0]); }}   

}

and there is another class function to get data from the http handler:

  httpAreviewer requestor = New HTTPAreviewer (); Id = requester.createUser (login); System.out.println ("The other class has an ID:" + ID); // This return empty   

Please help

Regards

Congress

Your approach is incorrect when you call new HttpAsyncTask (). Execute () It starts a new asynchronous operation (the egg is under a new thread hood). Execution method returns immediately and the results property remains blank. This is the reason that the createUser method is the reverse tap received method is synchronous, in the context of your program it started a different thread (thread with HttpAsyncTask ), But all the methods in it are constantly executed. This is the reason that the result property has been started here and has the right results.

  • Shared property ( result property) may be the root cause of this serious contemporary problem between different threads. HttpAsyncTask class To keep the portion of this property and return it as a result of the AsyncTask execution.
  • Result of post harassed method HttpAsyncTask class (which can not be applied here but can be overridden). This is the only correct way to ensure that you can ensure that the processing will be executed after the HTTP call has been made and the results have been received.

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -