This week's book giveaway is in the Java in General forum.
We're giving away four copies of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 and have ishori Sharan & Adam L Davis on-line!
See this thread for details.
Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Android not in the main thread when using web sockets

 
Greenhorn
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: 7236
169
  • 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:

runOnUiThread(this::startWebSocketConnection);



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.
reply
    Bookmark Topic Watch Topic
  • New Topic