• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

How can I get the value from the inside of a thread?

 
Ranch Hand
Posts: 69
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

I have created a new thread inside the onPostExecute method from the Async class. The thing is I want to return the value formed inside the thread. However, outside of the thread in the onPostExecute method, the value is not recognised. I want to call a method that returns the polylines, but without the value from the thread, I can't make it work. Also when I try to call the method inside the thread it gives an error : com.google.maps.api.android.lib6.common.apiexception.c: Not on the main thread. So I guess if I get the value from the thread and use it outside it won't give the error anymore and I will be able to finish my work.

 
Saloon Keeper
Posts: 12488
269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post the full exception stack trace.

Download the data in another AsyncTask. Run the method that throws the exception from another onPostExecute().

NEVER create your own threads. Use either ExecutorService or AsyncTask.
 
Marin Capranov
Ranch Hand
Posts: 69
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
02-17 15:52:09.351 15930-16917/com.example.authorisationfirebase E/AndroidRuntime: FATAL EXCEPTION: Thread-5092
   Process: com.example.authorisationfirebase, PID: 15930
   com.google.maps.api.android.lib6.common.apiexception.c: Not on the main thread
       at com.google.maps.api.android.lib6.common.m.b(:com.google.android.gms.dynamite_mapsdynamite@200414046@20.04.14 (040306-0):5)
       at com.google.maps.api.android.lib6.common.r.a(:com.google.android.gms.dynamite_mapsdynamite@200414046@20.04.14 (040306-0):2)
       at com.google.maps.api.android.lib6.impl.bi.a(:com.google.android.gms.dynamite_mapsdynamite@200414046@20.04.14 (040306-0):91)
       at com.google.android.gms.maps.internal.i.a(:com.google.android.gms.dynamite_mapsdynamite@200414046@20.04.14 (040306-0):150)
       at ch.onTransact(:com.google.android.gms.dynamite_mapsdynamite@200414046@20.04.14 (040306-0):4)
       at android.os.Binder.transact(Binder.java:387)
       at com.google.android.gms.internal.maps.zza.zza(Unknown Source)
       at com.google.android.gms.maps.internal.zzg.addMarker(Unknown Source)
       at com.google.android.gms.maps.GoogleMap.addMarker(Unknown Source)
       at com.example.authorisationfirebase.GetDirectionsData.showPolyLines(GetDirectionsData.java:114)
       at com.example.authorisationfirebase.GetDirectionsData$1.run(GetDirectionsData.java:77)
       at java.lang.Thread.run(Thread.java:818)
 
Marin Capranov
Ranch Hand
Posts: 69
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And if I create another async task that won't be a problem?
 
Stephan van Hulst
Saloon Keeper
Posts: 12488
269
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The exception is thrown by GetDirectionsData.showPolyLines(), which is not called by the code you've shown us.

Creating a second AsyncTask from the onPostExecute() method of a first AsyncTask should not be a problem.
 
Marin Capranov
Ranch Hand
Posts: 69
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah it's a different method but it is giving the same error.

SO you suggest creating an Asynctask what? Class or method inside the onPostExecution of the existing Asynctask. I was thinking I have to create another class Asynctask.
 
Stephan van Hulst
Saloon Keeper
Posts: 12488
269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Of course, if you're not updating the user interface in the onPostExecute() method of the first step, that begs the question why you're not performing the second long running operation in the doInBackground() method of the first step.
 
Marin Capranov
Ranch Hand
Posts: 69
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do something wrong?




E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #4
   Process: com.example.authorisationfirebase, PID: 9912
   java.lang.RuntimeException: An error occurred while executing doInBackground()
       at android.os.AsyncTask$3.done(AsyncTask.java:309)
       at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
       at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
       at java.util.concurrent.FutureTask.run(FutureTask.java:242)
       at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
       at java.lang.Thread.run(Thread.java:818)
    Caused by: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
       at com.example.authorisationfirebase.GetDirectionsData$1.doInBackground(GetDirectionsData.java:74)
       at com.example.authorisationfirebase.GetDirectionsData$1.doInBackground(GetDirectionsData.java:71)
       at android.os.AsyncTask$2.call(AsyncTask.java:295)
       at java.util.concurrent.FutureTask.run(FutureTask.java:237)
       at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
       at java.lang.Thread.run(Thread.java:818) 
 
Stephan van Hulst
Saloon Keeper
Posts: 12488
269
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're not providing enough arguments to your AsyncTask's execute() method. This is what causes the exception.

Why are you doing all that stuff in your onPostExecute() method? You can do everything in your doInBackground() method, except the call to showPolyLines(). Do that in your onPostExecute() method. Then you don't need a second AsyncTask.

Remember: doInBackground() is for everything that doesn't need to interact with your user interface. Use onPostExecute() only for interacting with the user interface.
 
Marin Capranov
Ranch Hand
Posts: 69
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you! I have fixed some issues, but I still struggle at understanding these threads and async.

I have created a new parser inside the onPostExecute but it returns an error: java.lang.NullPointerException: Attempt to invoke virtual method 'int org.json.JSONArray.length()' on a null object reference


Parser.class



MyAsync.class




I believe that the parser does not work onPostExecute and should a background task as you suggested earlier but it did not work in the doInBackground either.

What can I do?
 
Stephan van Hulst
Saloon Keeper
Posts: 12488
269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When posting exception messages, please post the full stack trace. The message by itself is usually not so helpful.

The problem has nothing to do with threads. You're just not passing data correctly.

Why is distUrl an input of your AsyncTask? Why are you overwriting it in doInBackground()? Why are you returning it again from doInBackGround()? If you're returning an URL from doInBackground(), then why are you treating it as JSON data in onPostExecute()?
 
Marin Capranov
Ranch Hand
Posts: 69
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for the confusion. I have achieved what I wanted. However, if you got any experience with direction API could you help me to decode the path that I have got from the overview_polylines object. I want to display on the map and it doesn't work.




Parser



the message from log "test" shows:

I/test: onPostExecute: [{obj={kd_IljoJhG`BpAuKjAmHhC}BhREnC}F?AAC?EmHmJtHdJDF?D?@JNnHxJj@jHu@`OuB|DoIrCyHaEkFkBsIcCJmFdBkLlCyNrBwOcC]A@?@A@A?C?CGyC}@IGyKyDwDTd@[tPrD?RE?uJzd@`@zCq@vHbHjBnNbEtCrAjIbD|HsCfP~BrS|VtEcBdJnDnK[mHs@lFaKgF{PgGsI{Fc\]eBoGoRx@ab@iBwC{F]_CTCFKBIQQu@g^oWoGoDqKv@_H`GrF_F~SqMfEpQpHhGrPzNJAFN?HfBK`KhBmAxc@fG`Qx@dC?TvFh\~B~FjFj@bJzGNKJR|ClVwBGfG\nC~HDqOoAsVABC@C?EKDKzEaPiJgQkPwPoJaKsN{F[oc@lF{QAK}F_q@oL`@iMhFe@_GlBrErMaFbEeHbAcYdBc^qU{FiIPqEoAcK|\aIjL{JrYk@|FaFxSyC_H_WuDoVmCyLiF{G_YiEfNlDvE`InA`@`@dJfA^tDk\xdA_G`TcJ{@GvD~KtIzKdNnUje@bIfEm]roB}Qlm@|TpNdTnDnK`QzLb[fFpQnEfPg@vHlGdHzIvK@AF?xBgGtBqCpApAdFhBhAbBcJ}FgF|Ha@`A@HILkQ`KsEbAs_@yF}QReKoLwOahAsDyK{@`CxBjc@h@xKtF}LiDoYsDyKsAxAwBwDhHyYzUct@nQ}gAdJgh@kCoEmVsb@{D}G}VoUqCiCjIiFxM{NdNe[~Kil@fNsWdJiYr@kS}Vun@kBfD\zGIcEn@gCl@jFqApBUyIvAmHrAr@xKl\~Hx_@yLrd@mKtYeYh_AsGtUeDdDaFuAcCpB`IfHvPxPpRta@rJtHq@|JkX|wAaFhZkLh[gJ|j@kBbEoCqJcC{GuArEJbEx@`AHj@c@xEfCrBz@qDr@gCHkAhC{A~C}VzUct@nQ}gAdJgh@kCoEmVsb@{D}G}VoUqCiCjIiFxM{NdNe[~Kil@fNsWdJiYr@kS}Vun@jC}Zp@}AlGrCbGnFjNrJvU_CjFFpFm@pKxKzWvHPGxYtHl]tU`G}@fBrGlAx`@~DbWl@`T_Fhw@tS~j@lC~GtWd_AFn@QAiG|T{Brm@DH?JKFu@jJpCtQ~HrWnIlTpBlKqBmKwGqFmBN{\ePCFI?CQkHqDaMgGwIsEqJaAyLhEoPmHmByKwXaIw_@wOgIeLsGaGmDmEvBsZfAkOmCaBcBSsMvHwPj@mOmVoUwb@_TcM_AgI}TilBRzB~ArS|Gla@hHxm@d^|_@xJvXpQlTnEfEtJSrMwHpIlB}]


 
Stephan van Hulst
Saloon Keeper
Posts: 12488
269
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  • There really is no point in starting up another AsyncTask in the onPostExecute() method if that's all you're gonna do. Put the code of the inner doInBackground() at the end of the outer doInBackground(), and replace the contents of the outer onPostExecute() with the contents of the inner onPostExecute().
  • The distObj variable is unused.
  • Why are you continuing execution of doInBackground() if readUrl() throws an exception?
  • Why are you passing a variable named url1 into a method that expects a JSON string?
  • Why is a parameter of type List named 'string'? Use a descriptive name. What does the list represent?
  • Don't use HashMap as your type argument. Use Map instead.
  • Instead of parsing JSON by hand, use a JSON binding API such as Jackson or JSON-B. Or use the Google Maps Services Java Client.

  • As for decoding the polyline, the developer manual is here: https://developers.google.com/maps/documentation/utilities/polylinealgorithm

    Instead of decoding the polyline manually, you may want to use the Google Maps Services Java Client. Running the client directly on Android is not recommended, as per the developer manual, but if you REALLY want to send requests directly to the Maps service instead of through your own proxy server, you can probably use the PolylineEncoding class to achieve what you want. The client library can be found here: https://github.com/googlemaps/google-maps-services-java
    reply
      Bookmark Topic Watch Topic
    • New Topic