• 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

How to pass string from TCP server (a Runnable) to main activity (in Android)?

 
Ranch Hand
Posts: 52
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Please help me with passing a string from TCP server to the main activity.

The code for server and main activity is below.

Thank you for any suggestion and time spent on this.

Cheers,

Madhu Nandan


TCPIPServer



TCPIPCommn (the main activity)

 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This looks like a strange design. The different parts of an Android application don't communicate with one another using sockets - they use Android mechanisms like startActivityForResult, AsncTask or a common Application subclass. What exactly are you trying to achieve?

As an aside, you should read Don't println to a Socket. Since the code uses "readLine" it is potentially vulnerable to this problem (although this is not an issue if both client and server run in the same JVM).
 
Madhu Nandan
Ranch Hand
Posts: 52
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:This looks like a strange design. The different parts of an Android application don't communicate with one another using sockets - they use Android mechanisms like startActivityForResult, AsncTask or a common Application subclass. What exactly are you trying to achieve?

As an aside, you should read Don't println to a Socket. Since the code uses "readLine" it is potentially vulnerable to this problem (although this is not an issue if both client and server run in the same JVM).



Thank you for the reply.

First of all I am new to Java and Android and I may not be doing things in the ideal way. Anyway, I will try to explain my situation.

I will have an app on Android phone which will communicate with another desktop app of my colleague which is a Rhapsody based app (call it ABC) sending CAN messages over either bluetooth or TCP. The app in Android phone will receive the messages and display an image or some driver assistance information.

Communication via bluetooth had some problems as ABC sends data over SPP profile and the bluetooth SPP profile in the phone I am using (HTC Desire, 2.2 Froyo) has a buggy implementation. So trying TCP now.

With TCP, I will run either the server or the client on the phone (I am not sure which is better on phone), receive messages from ABC and do something as mentioned above.

Coming to the problem in this code, I can send and receive messages between client and server (can see LogCat as proof for communication). Now I want to pass the string received in the server to the main activity (where I have functions for parsing the string and extracting it's name and value which is used further for providing driver assistance info).

But I am not aware of how to pass this string to the main activity. I hope I have been able to explain my problem.

Please feel free to suggest any other changes, if any, for the whole way of doing things.

Cheers,
Madhu

Update:
I read it is better to use Callable instead of Runnable to pass data from threads. But I am not sure how to do this. Would appreciate if any info is given on this.
 
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator






 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, so one only side of the communication actually runs on the phone. In that case the article I linked to is indeed relevant.

It would be the client that runs on the phone, as there generally is no way for desktop apps to reliably access a server process on a phone.

(Note that you need to use the "magic" IP address 10.0.2.2 to access the host if the code runs on the emulator instead of an actual device.)
 
Madhu Nandan
Ranch Hand
Posts: 52
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying the above code.
 
Madhu Nandan
Ranch Hand
Posts: 52
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I followed your code and tried the below thing:


Swastik Dey wrote:







I get the following things in LogCat (including errors).



I would be grateful if you could help me with this.

Madhu
LogCat1.jpg
[Thumbnail for LogCat1.jpg]
LogCat
 
Swastik Dey
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The exception is probably at this line double msgValue = Integer.parseInt(actVal); It means you are getting some response but actVal can't be converted to numeric. So just check what you are getting as response, and what you are getting in actVal after the split operations.
 
Madhu Nandan
Ranch Hand
Posts: 52
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Swastik Dey wrote:The exception is probably at this line double msgValue = Integer.parseInt(actVal); It means you are getting some response but actVal can't be converted to numeric. So just check what you are getting as response, and what you are getting in actVal after the split operations.



I am a bit confused (and I am sure I am wrong somewhere). I think I have coded this as a server which will start running and expect a message. At the moment, I am not able to get the server itself running. In the LogCat, there is error after "S: Connecting..." So I though there is some error in this part-TcpServiceHandler.

(After the server is running, I will run client as a Java app and send message in this format: <MSG><N>xxx<!N><V>yyy<!V><!MSG>. This message should be received by server and parsed).

Thank you for your time.

Madhu
 
Swastik Dey
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To be honest, even I am also confused. Earlier you had said your android app is supposed to connect to a desktop app, so as per me that should be server, correct me if I am wrong.
 
Madhu Nandan
Ranch Hand
Posts: 52
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Swastik Dey wrote:To be honest, even I am also confused. Earlier you had said your android app is supposed to connect to a desktop app, so as per me that should be server, correct me if I am wrong.



The Android app runs in the phone and waits for a message. The other app tries to connect to Android app and sends a message. The Android app receives it and processes it. So I called the Android app as server and the other as client. Is this way wrong?

And is the way I am trying to connect in TcpServiceHandler ok? I guess there maybe some mistakes there.
 
Swastik Dey
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As far as my knowledge goes, to connect to a server you need an ip address/dns. So does the desktop app know anything about phone's ip? Typically phones get a dynamic ip address. And what's wrong making it a client? Send a message to the desktop app(server), server sends the xml back to you. Now process that xml. Your xml parsing does not also seem to be too good. Why not use standard parsers like sax/dom?
 
Madhu Nandan
Ranch Hand
Posts: 52
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Swastik Dey wrote:As far as my knowledge goes, to connect to a server you need an ip address/dns. So does the desktop app know anything about phone's ip? Typically phones get a dynamic ip address. And what's wrong making it a client? Send a message to the desktop app(server), server sends the xml back to you. Now process that xml. Your xml parsing does not also seem to be too good. Why not use standard parsers like sax/dom?



The android app has to receive the message and the parsing has to happen in the android app itself. After parsing, based on message name and value, an image or some driver assistance info has to be displayed on the phone.
 
Madhu Nandan
Ranch Hand
Posts: 52
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Swastik Dey wrote:As far as my knowledge goes, to connect to a server you need an ip address/dns. So does the desktop app know anything about phone's ip? Typically phones get a dynamic ip address. And what's wrong making it a client? Send a message to the desktop app(server), server sends the xml back to you. Now process that xml. Your xml parsing does not also seem to be too good. Why not use standard parsers like sax/dom?



Hi,

I tried as you said. The server in the desktop and client as android app. I think the client is not able to connect to the server. Please go through the code below for server and client and suggest corrections.

TCPSendServer:


TcpServiceHandler (based on your suggestion earlier. The interface and main activity are also similar to what you suggested earlier)

 
Swastik Dey
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Madhu,

Your code worked for me without any issues. Make sure the 1)server ip address you are using in client is correct. 2) Server is running. 3) You have added the required permission i.e.
android.permission.INTERNET in android manifest file.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I notice that the code still has the Don't println to a Socket bug (on both ends of the connection, actually).
 
Madhu Nandan
Ranch Hand
Posts: 52
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Swastik Dey wrote:As far as my knowledge goes, to connect to a server you need an ip address/dns. So does the desktop app know anything about phone's ip? Typically phones get a dynamic ip address. And what's wrong making it a client? Send a message to the desktop app(server), server sends the xml back to you. Now process that xml. Your xml parsing does not also seem to be too good. Why not use standard parsers like sax/dom?



As I have told earlier, I have not done many things in the best way... I am not aware how to use SAX/DOM parsers for my case. I get a string in this format: <MSG><N>shiftDirection<!N><V>1<!V><!MSG>

I have to extract the name (shiftDirection) and value (1) from this. So I went for simple pattern and matcher classes which worked for me.
 
Madhu Nandan
Ranch Hand
Posts: 52
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:I notice that the code still has the Don't println to a Socket bug (on both ends of the connection, actually).



Hi Ulf,

I just browsed through the link you suggested... I am not sure if I completely understood what you mean? If I am not wrong, you say don't use

str = in.readLine();



Could you please suggest a better/alternate way?
 
Madhu Nandan
Ranch Hand
Posts: 52
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Swastik Dey wrote:Madhu,

Your code worked for me without any issues. Make sure the 1)server ip address you are using in client is correct. 2) Server is running. 3) You have added the required permission i.e.
android.permission.INTERNET in android manifest file.



Hi Swastik,

I had done the first two steps but had not added internet permission in the manifest file. Now I added that and the code is working.

Thanks a lot.

Madhu
 
Madhu Nandan
Ranch Hand
Posts: 52
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:I notice that the code still has the Don't println to a Socket bug (on both ends of the connection, actually).



Hello,

Could you please explain what is the problem-causing part (I guess in.readLine) and could you please suggest the alternative?

When I ran the server on my laptop and client on android phone last week, it worked perfectly. But when I try the same today, I get the following error in LogCat (in Eclipse):
"Tag: SntpClient; Message: request time failed: java.net.SocketException: Address family not supported by protocol"

If I run both server and client on laptop (client in Android emulator), I do not get any error; everything works fine. Problem occurs only when client runs on real phone.

Thank you for your time and effort.

Regards,
Madhu
 
Madhu Nandan
Ranch Hand
Posts: 52
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:OK, so one only side of the communication actually runs on the phone. In that case the article I linked to is indeed relevant.

It would be the client that runs on the phone, as there generally is no way for desktop apps to reliably access a server process on a phone.

(Note that you need to use the "magic" IP address 10.0.2.2 to access the host if the code runs on the emulator instead of an actual device.)



Hi,

I just browsed through the link you suggested... I am not sure if I completely understood what you mean? If I am not wrong, you say don't use
str = in.readLine();
Could you please suggest a better/alternate way?

Madhu
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic