I want to create a pair of Android app (Server - Client) using Socket to communicate with each other in real time. I found the sample here http://androidsrc.net/android-client-server-using-sockets-server-implementation/ which works. The client can connect to the server. Now I think about making the server a... "web server" (I don't know if I'm calling it the right way) that can display what they (server and client) are communicating (maybe showing their chat or calculation...).
But when I type in the address and port (eg: http://172.16.1.21:8080/) into the browser (Chrome), the result is
172.16.1.21 sent an invalid response. ERR_INVALID_HTTP_RESPONSE
I guess it was responding but with some other kind of format that the browser doesn't understand. I tried calling with postman and got the error:
Could not get any response There was an error connecting to
172.16.1.21:8080.
I tried putting an index.html to the root of the storage (/storage/emulated/0/) but no luck
The idea would be the same as the app "Websharing" on store. All you need is that the phone and the Computer is on the same network. When access the app, the browser will show the content of the phone.
My idea is to show other information. I though about creating another "display-app" to show the result, but then what if I wan't to send some custom message to all client, then I will need an another phone, which is so difficult to have. Opening another tab is much easier. I don't want to risk using online service like Firebase because the internet connection may not be stable. Just local connection is fine for me.
I don't know that is it called, a keyword would be appreciated. I tried googling and found some suggestion that to run an app called "Tiny web server", I haven't tried it but I don't wan't to use a third-party app because the installation will be complicated and not support full customization.
Thank you for your time.
Related
I have started working on an Android app for which we need to use MySQL as database and Ruby on Rails for server side code. We will be using SQLLite too on device(will sync both DB as and when required). I searched the web and couldn't find any relevant tutorials/examples which can serve as a base to start with.
I have gone through MySQL and ROR tutorials but still has confusion on connecting Android with ROR.
Can somebody share some relevant tutorials/code snippet which can explain the complete linkage of the technologies. I mean how to send data from Android device to MySQL and vice versa. I know the concept theoretically but not sure how and where to start with.
My sincere apologies for asking such a basic question or if I sound ambiguous but I am a beginner and need to complete this task. Thanks in Anticipation..
Here is a brief overview of what you should know to accomplish your goal. I am not going to go that far into detail, especially since I have never personally used RoR. Note that some of these parts might not relate exactly to RoR, but the general idea behind it still applies. I will leave it up to you to research and figure out how to implement each individual component.
The general flow of everything is as follows:
Android App <==> Network <==> Web Service <==> MySQL
Note the double-edged arrows since data will be flowing in both directions.
The Android App is the client, and the Web Service and MySQL database are located on your Web Server. I only included the Network part for completeness, but you shouldn't have to do anything once the data has been sent onto the network.
A brief overview of each section:
Android App:
The Android App is the client that sends and retrieves data from the Web Server. I am assuming that in your app you are going to allow the user to do some tasks which in essence becomes the data that you want to send to the server at some point.
Take for example, the user should be able to enter his name and favorite animal. Lets say that there is an actual "Submit" button that the user may click. When this "Submit" button is clicked, it should wrap up the data into a proper format to be sent across the network. Two of the most common ones are JSON and XML. Once the data has been formatted properly, you will want to send the data to the server using some type of network protocol such as HTTP. In order to send the data, you of course must have some URL as the target. Lets say the target is www.example.com/webservice.php. This target is our Web Service located on the Web Server.
Once you send the data, the server will respond with some data at which point you can do whatever you want with it. Maybe display it to the user, or stick it in an SQLite database, or even both.
The key thing to remember is that there is no magic going on. Everything I have just described will be implemented in Java code that you will write in your Android Application at some point.
Key Ideas you should research more and figure out how to implement in Java code:
JSON and XML
HTTP in Java
REST and SOAP
Here is an excellent video on possible ways to set up the structure of your Android App.
Make sure that you are doing all network operations in your Android App on a different thread. An easy to use method is an Intent Service.
Web Service:
This is often the most confusing part. A Web Service is simply some entry point for clients attempting to access the Web Server. My explanation here might different slightly when using RoR, but the same idea applies. Notice above that the target URL was www.example.com/webservice.php. The web service is literally the PHP code that exists on the Web Server, called webservice.php. In your Android App, when you send data to the target URL using HTTP, the Web Service code will be executed on the server (and also have access to the data that you sent to it). Inside of your Web Service code, you will basically be extracting the data (which is in some format like JSON), grabbing the necessary parts, and then doing something with it. In this case you will most likely be querying the database. In PHP it is easy to write code that connects and queries a MySQL database that is also running on the server. When the response of the database is retrieved by the Web Server, you can send it back to the Android App. Just as before, remember, there is no magic going on. All of these ideas are implemented by writing some code.
Main ideas to research:
Ruby on Rails web service
How to access a MySQL database using Ruby on Rails
MySQL Database:
This is where you will store the data on the Web Server. I am not going to go that in depth here because this is just going to require you doing a lot of reading up on how to set up a MySQL database on a web server. It is also important that you learn how to create the appropriate queries such as SELECT, INSERT and so forth.
Main Ideas to research:
How to setup a MySQL database on a web server
If you need any clarification, let me know!
I followed the demo client/server tutorial that Android docs have provided (http://developer.android.com/google/gcm/demo.html). Everything worked exactly as advertised when I first tried it out. The servlet first came up with "No devices registered!" screen, then started the demo app on my device and I was able to send the demo messages to the device just fine.
Then I shutdown my Tomcat server that deployed the gcm-demo war and closed the demo client app on my device (which is not an emulator, Nexus 7). I started the tomcat server back up, I see it did deploy the gcm-demo war as expected however when I go to the URL of the servlet (the one that originally said "No devices registered!") it now says that No devices are registered. I opened up the app on my device and it says that device is already registered on server. Why is the servlet not seeing my device that IS registered?
I appreciate any help, thank you.
I didn't take the Tomcat/servlet approach myself, opting for an Apache/PHP/MySQL way instead, so I've no direct experience, but reading the sample code I see as the comment in the server's Datastore.java, this:
/**
* Simple implementation of a data store using standard Java collections.
* <p>
* This class is thread-safe but not persistent (it will lost the data when the
* app is restarted) - it is meant just as an example.
*/
So basically when you restart Tomcat it's forgotten your regId.
#NickT is right. If you want persistent storage of regIds you can back it up with a java.util.Properties that stores the data into a properties file or write a simple file store yourself.
Reset the state of your client app by deleting the cache or re-installing the app.
Hello every one I am new with servers things. I want to develop program that get data from the database on the server and it's my first time I don't have any idea of this thing.
Let say like Login & Logout:
how can i make a virtual server for testing on my PC
how can i connect the emulator with this virtual server
how can i request the data from thee database
Do I need software like Xamp or Wamp? If yes how can I use it?
I found so many examples about the json and webserver but I don't know how to make virtual server and connect the emulator to it.
as I understand this problem you need two separate pieces of software. You will need 1) server. To begin with you can install XAMP and WAMP to get Apache running. There are millions of tutorials on how to set up an Apache web server online, and to begin with you could just return a string or a simple data structure (e.g. JSON or XML). As you want to do more complex things you can learn more, but in the beginning think "easy does it". From your text I anticipate that you just want to test as a proof of concept.
- What OS do you run BTW?
Another solution is to rent some space on a server accessible from the Internet. Then you could test your server-side regardless of where you are located.
Second you would need to create a program for the android. There are several guides, but have a look here: http://developer.android.com/training/basics/network-ops/index.html. If you choose to make your data available through the HTTP protocol there are very boilerplate-like procedures for how to download and parse the data.
If you are hosting the server-side on your local computer you would need to use local addressing, but if you choose to put it online you could acquire the data from everywhere as long as you have an internet connection.
Good luck!
Below I explained what I need and in the answer I would like to get information what technology, what kind of protocols, services etc should I use.
Also I know that there is a massive amount of information on the internet, but because there are so many choices I'm unable to make a decision. Here's what I want to have:
Android application which will sent and receive information from the internet/server
Of course server, which will do some rudimentary computation with the obtained information, then store it and at the end send it back to the client application
I have to admit that the subjects of networking, socketing, protocols, ciphering etc are the ones I've always run away from. Therefore advices such as what domains, databases etc utilize are highly welcome
Update: After a bit of a research I've implemented mechanism based on information from this site [1]. I already have a working MySql database with several tables inside. Also I've added php scripts to my Apache webspace and implemented all that's necessary within my Android app.
Now, as I understand communication between php's scripts and MySql database is safe (I've hardcoded the usr/pass within scripts). Therefore the only thing that has left to be done to secure the connection between my Android application and php scripts (I want to prevent the situation when everybody has an access to my php scripts). So my question should be pretty straightforward now, how can it be achieved?
[1] http://blog.sptechnolab.com/2011/02/10/android/android-connecting-to-mysql-using-php/
I think a simple TCP/IP connection via Sockets will do just fine for your purposes.
The lesson (with more information and some examples) from the Java-Docs can be found here.
The basic workflow is as follows:
Your Android-App opens a Socket-connection to your server on a
given port where a server-application listens.
The Android-App sends it's data (whatever that might be) to the
Server.
The Server reads the send data,
processes it,
stores it...
...then it sends back some response (maybe the computed values) to your
Android-App.
Your app can then figure out if everything went okay and use the
given data.
Answering my second question, all the php scripts has been put into directory with the password on my Apache server. To be able to trigger them I need to give this password which is hardcoded in my application. All the connection is done through HTTPS. Now everything works as planned!
I have an Android App which uses http communication for nearly every operation. I want to be able to demo without connection to the internet by somehow replaying the http exchange. How can this be done? So I want to somehow almost like mock objects but really mock http session so I can always demo the app on or offline. This is really a very cool thing to be able to do. Since you can demo the app easily and reliably. Does anyone know how I could do this. Replicating the whole server side is just not an options its got too much stuff. Its important not to just show screencast but the real data exchange. I just want to be able to run thru the app and replay. Maybe debug as well. Thanks
Here's a hybrid solution using similar ideas from other answers:
You could write a dead simple HTTP server that listens on "localhost:80" (or whatever the port is on the server you're targeting) and point your application to this host instead by factoring out the host name from requests. Your local server has a reference to the actual remote server and does the following:
If ONLINE, forwards the request as-is to the real server, gets the response, saves it locally either in an in-memory cache keyed by the request URL or a file named with the URL as its identifier (munged appropriately)
If OFFLINE, looks up a request in its (in-memory or file system) cache and returns the contents from the cache instead
This is kind of like the record/playback mode that #nicholas.hauschild says.
Now you can just run your app once when ONLINE, causing your localhost server to save away requests that it issues against the real server. Then when you run your app OFFLINE, it just returns these cached contents instead whenever the same URLs are issued.
Hope this helps.
If you're device is rooted, you can use tcpdump as explained in this post: http://www.vbsteven.be/blog/android-debugging-inspectin-network-traffic-with-tcpdump/
or use AndroShark (get if from xda-developers here: http://forum.xda-developers.com/showthread.php?t=725692)
or this one (wifi only): http://www.9bitlabs.com/default.aspx
I would create a "Record Mode", and a "Playback Mode" for my app.
While in Record Mode, I would write out a file each time an http request was made. The file would be named by the endpoint the request is made. The contents of the file would a collection of serialized http requests/responses broken up by line. You could then deserialize lines from this file until you find the proper request, and play back the deserialized response.
This approach would also allow you to create Record/Playback profiles, where you could record multiple different sessions (by placing the files into a different directory) and then playback from whichever profile you choose.
This whole approach could be done with a small wrapper class around the HttpClient object you are using.
One way would be to use an HTTP proxy. Redirect all web traffic to the proxy, which can be running locally on the phone. This could be done with little or no source code change.
find a way using fiddler on pc,and android app take fiddler as proxy.So the http traffic is record.
http://blog.csdn.net/grhunter/article/details/5830199
Simples solution lies in faking it when there is no connection. If there is a error in connection, make sure ur app throws some preset data rather than an error in connection thing.