Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • 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 ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Android not in the main thread when using web sockets

Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an app where I have to use websockets to update markers on google map in real time. I successfully did the connection with web sockets and in the response I'm getting the json string. I parsed to JSONObject and when I debug I see that all the data in the json are coming but the problem is when I have to add markers. The error is:

After connection to websocket I did the following code to get the data:

and I run the websocket in the runonuithread() on onCreate as in the following:

I'm using okHttp library to perform communications with websockets. What I'm doing wrong here. Does anyone have any suggestion what I need to improve. Any help will be appreciated. Thanks in advance.
Saloon Keeper
Posts: 6650
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

This looks wrong:


Any network connectivity must not be done on the main (UI) thread, but in a background thread. Sometimes it works, but you need to be prepared that it does not, and is a bad idea to begin with.

On the other hand, the setting of pins sounds UI-related, so it must happen on the UI thread - does it? The error message sounds like it does not.
    Bookmark Topic Watch Topic
  • New Topic