• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
  • Piet Souris
  • Himai Minh

Android not in the main thread when using web sockets

Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 7327
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
Evil is afoot. But this tiny ad is just an ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
    Bookmark Topic Watch Topic
  • New Topic