Saturday 15 September 2012

android - how to pass parameter through async task? -


I am trying to post toast from an async job and am reading this reply on the stack:

Summary leads to:

Get context objects by calling getApplicationContext () from main activity and pass it as parameters on your AsyncTask

I have to pass a reference through the async work and how to call it the illusion:

My async Ary is:

  public class ReadLogInJSON Async Task & lt; String, zero, string & gt; {@Override protected string doInBackground (string ... arg0) {// TODO auto generated method stub returns Jasonfid (RG 0 [0]); } Protected Zero PostExecute (string result) {// decode Jason Try Here {JSONObject json = New JSONObject (Results); String position = json.getString ("status"); If (status == "no") {// toast login unsuccessful string message = "login failed"; Toast. Make Text (this, Message, Toast. LNGHHARAR) Show (); } Else {// get usernameName // get user id // set preferences // launch general activity}} cache (exception e) {}} public string readJSonFeed (string url) {stringbilder stringbillder = new stringbilder (); HTTP Client HTTP Client = New DefaultHttpClient (); HttpGet httpGet = New HttpGet (URL); Try {HttpResponse response = httpClient.execute (httpGet); Threaded Positions = Response .getStatusLine (); Int statusCode = statusLine.getStatusCode (); If (statusCode == 200) {HttpEntity entity = response.getEntity (); InputStream inputstream = entity.getContent (); BufferedReader Reader = New BufferedReader (New InputStreamReader (InputStream)); String line; While ((line = reader.readline ()) = faucet {stringBuilder.append (line); } Inputstream.close (); } Else {Log.d ("JSON", "Failed to download file"); }} Hold (exception e) {log d. ("ReadJSONFeed", E..T.O.Localized Message ()); } Return stringBuilder. Toasting (); }}   

And I call it on:

  New RidLog InJesson (). Executed (url); You are trying to toast in poststick that runs on the main thread:    

So that you do not need to pass the reference for your work, you should be able to do this:

  Toast MakeText (MyActivity.This, Message, Toast.lNGTH_SHORT) Show ();   

You can get getApplicationContext ()

  toast. Can also use make text (getApplicationContext (), message, toast .LENGTH_SHORT) Show ();    

No comments:

Post a Comment