• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Multi-threading Android

 
Ranch Hand
Posts: 65
Android Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a server call to fetch JSON response. Here is the code snippet.


convertStreamToString(InputStream) read json text from the stream and returns a string text. For holding the state of the user session, same httpClient reference is being used.



The problem arises when a service starts working. It accesses the httpclient object and in turn InputStream instance. By the time, the user moves to other screen, putting service to run background. Exception is thrown when InputStream already in use is called for.



The reason being, the socket is still used by the Service class. I have heard of ThreadSafeClientConnManager class. However, not really sure if it suits in here.

Any thoughts?

Let me know in case you need more details.

Thanks in advance!
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since the javadocs don't mention that DefaultHttpClient is thread-safe, you should assume that it is not. Use a separate instance for each concurrent client.
 
Renjith Mohan
Ranch Hand
Posts: 65
Android Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes I am. I use AsyncTask for each function. The issue seems to be here



I am not creating new HttpClient instance for every server operation. Instead use the same instance to hold user session details. On the other side, ThreadSafeClientConnManager allows me have a multi threaded environment. How could I manage user session?

Thanks in advance!
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

How could I manage user session?


By using separate HttpClient objects for each user.
 
This is my favorite tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic