I want to connect to my Azure DB from Android. From Android side im using JDBC:
Connection con = ConnectionJDBC.connectionClass();
String query = "SELECT * FROM mytable";
Statement stmt = con.createStatement();
stmt.executeUpdate(query);
From azure side, my server is normally up and running and the firewall settings allow IPs 0.0.0.0 to 255.255.255.255. Everything else is unchanged.
That also works for most WiFi networks over which I made the request. However, it doesn't work for my university's WiFi (specifically ETH Zurich), con will just be null. Does someone know what the problem could be? I think it lies in the configuration of the Azure Server but I don't know what else I could change.
Please see JDBC vs Web Service for Android, as suggested by Morrison Chang: JDBC should not be used and is sometimes blocked. Instead create a mobile app service in Azure (they will change the name to web app service), and connect to that server, as suggested here: https://learn.microsoft.com/en-us/azure/app-service-mobile/app-service-mobile-android-get-started
I've been looking for a way to connect an Android application to a SQL server. Tried different codes and asked many programmers. Didn't get anywhere. It seems that the problem is in the connection url.
The application I got to make involves creating some sql tables. For instance, at the beggining it needs to veriffy login data given by a user against that data in one of those tables.
In addition to Android studio, I was guided to install and use SQL server express (nothing else).
Using SQL server express 2008 R2 I created a database on my computer and a table. The connection to the server didn't work. The codes I found always involved a password and a username for the connection. Is that mandatory? I didn't get a username for the connection when I installed the server. It involved getting an instance name and a password.
I searched for information on the connection url's parameters in order to fix the url I got. Couldn't find what I need to change.
I found a code thoguh which one can define a password and a usename:
private Connection conn;
public MySQLConnection() throws SQLException {
System.out.println("At MySQLConnection()");
String driverName = "com.mysql.jdbc.Driver";
try {
Class.forName(driverName);
Properties props = new Properties();
props.setProperty("user","fred");
props.setProperty("password","secret");
props.setProperty("ssl","true");
String url = "jdbc:mysql://zivpc/workers?user=fred&password=secret&ssl=true";
Connection conn = DriverManager.getConnection(url);
this.conn = DriverManager.getConnection(url);
System.out.println("connected");
} catch (ClassNotFoundException e) {
System.out.println(e);
}
Trying to connect results in the following exception: CommunicationsException: Communications link failure.
Thanks!
You will need an API (REST, etc.) running on a web server, with that web server having access to the firewalled-off database server. And authentication for your API so that you don't have random people DDoSing your service and database.
Your database server should not be exposed to the internet at all, nor directly accessible from your client. Nor should you be sending SQL queries directly from the client through to the database server. Your API exposes a set of functionality and is very restrictive about what the client can request and receive.
Mobile app -> internet -> web server -> firewall -> SQL Server
The details of "how do I implement this" far exceed the scope of Stack Overflow. There are numerous tutorials and walkthroughs available online. Choose wisely.
Additional note: You say you're using SQL Server 2008R2 Express Edition. Be advised that this version of SQL Server will be EOL in less than three months. No one should have been doing new development on it for the past 5 years. Be careful taking advice from whoever "guided" you to install this version in 2019.
Hi I created a SQL management Database, I wish to connect to the Database through Android App
I am not what I did wrong, I am not able to login.
This my SQL management Studio 2012, My DataBase name I call it DB.
This is my SQL Server Configuration manager, what I did is I enable TCP/IP and shared memory.
And then I enable all the IP, and I set the ALL port to 1433, the dynamic port I leave it empty.
I create the database using this username
This is what my code is like for my Android Java file, connection to database
String driver = "net.sourceforge.jtds.jdbc.Driver";
String connString = "jdbc:jtds:sqlserver://127.0.0.1:1433/db;encrypt=false;
user=username;password=1234;instance=SQLEXPRESS;";
String username = "username";
String password = "1234";
int id;
Is it something wrong with my step, how come it is not connecting?
Connecting directly to a DB from android is highly discouraging. The primary reason is performance. See, it is really expensive to keep a remote connection alive than rather just call Web Services on demand which is more portable and also more secure.
Therefore, its better to develop a REST API instead for your service and invoke it from client android app or any other client for that matter.
Edit: For your current issue at hand, see if it works by replacing localhost IP to the real(assuming 192.168.0.16 is also ipa4 ip?) one.
String connString = "jdbc:jtds:sqlserver://192.168.0.16/db;encrypt=false;
user=username;password=1234;instance=SQLEXPRESS;";
Is it possible to host a web server within an Android phone itself?
Similar to Apache. I want to access the web server from the mobile
browser itself.
Couldn't find any reference online. Is this possible?
Thanks in advance
Short: yes. If you are looking at code, check out the org.apache.http package. If you are looking at a ready app, check iJetty and kWS.
KSWEB — a suite for web development on the Android platform. It consists of: a web server, PHP programming language and the database MySQL. KSWEB allows you to organize a platform for running and debugging Web applications (sites) in your Android device. Working with the application is very simple. To start the server it is enough to run our application, select, if necessary, port and the root directory.
By default, KSWEB contains a fully functional configuration file of the server, PHP and MySQL. However, if you want something to change them, go to the server options and click on the item «INI Files». Configuration files will be moved to SD-card of your device at «/mnt/sdcard/ksweb/ini/», if available. If repeatedly clicking on the item settings «INI Files» KSWEB will use the internal configuration files.
What's in the plans?
Review the requirements for the Android operating system, in particular
reduce the requirements for the version of Android to run the application to 2.0;
Add the ability to track the flow of errors arising when working with php and mysql;
Move the log files on the server and mysql on sdcard;
Extend the additional PHP libraries. For example, pdo_mysql;
Move the database files to MySQL on sdcard.
https://play.google.com/store/apps/details?id=ru.kslabs.ksweb
http://www.kswebserver.ru/
Atjeews android app server, small footprint, biggest advantage for me was jsp support.
I recently installed BitWeb which I recommend highly. I have a rooted phone with lends me a few more options (and I also recommend rooting if you haven't and plan on doing any server tinkering at all)
BitWeb is nice in that it includes a lighttpd server, a stripped down php engine and a stripped down mysql engine giving you a quick and dirty LAMP system with just one app. I use mine in conjunction with an sshd app to gain a busybox shell, I installed nano and use the ssh connection to do an sshfs share to edit content from my pc.
Bitweb includes the ability to tweak the config settings for all three services, and I've even managed to get multiple Zend servers working on just the one little server with a little tweaking to the re-write rules and the application.ini settings.
https://play.google.com/store/apps/details?id=com.andi.serverweb&hl=en
If you want to see some of the config options I had to customize, I can either post them or respond via email with what I set up as well as some comments on the pitfalls I ran into along the way. (I recently posted one as a self-answered thread on here by the way, although it turned out not to be specific to the android platform but with lighttpd/Zend)
I'm just bummed that t-mobile/HTC appears to have no plans to upgrade my phone beyond Gingerbread as I understand some of the new android OS's have ways you can access some of the hardware (such as the cameras) from scripts or compiled code without a fuss. If I want to do it now, I either need to hard code my own interface to them or get a pre-roll app capable of doing it which really tasks my phone when running side-by-side with the web/mysql/php services.
I might be a last to answer this question but believe me, this is the simplest and latest answer in 2020 for those who need to start a webserver in Android and need to serve multiple clients without implementing any other third-party libraries.
Copy your HTML file into the assets folder of Android as server.html and declare the serverSocket as global in your class.
Run the below code inside a new thread.
try {
serverSocket = new ServerSocket(8080);
while (true) {
final Socket socket = serverSocket.accept();
new Thread(new Runnable() {
#Override
public void run() {
try {
BufferedReader is = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter os = new PrintWriter(socket.getOutputStream(), true);
String request = is.readLine();
Scanner scanner = new Scanner(getAssets().open("server.html"));
String response = scanner.useDelimiter("\\Z").next();
os.print("HTTP/1.0 200" + "\r\n");
os.print("Content type: text/html" + "\r\n");
os.print("Content length: " + response.length() + "\r\n");
os.print("\r\n");
os.print(response + "\r\n");
os.flush();
socket.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}).start();
}
} catch (Exception ex) {
ex.printStackTrace();
}
That's all your native Android Java (no third-party libraries implemented) webserver is live at port 8080.
If you want to use DB and POST requests, try to implement SQLite and WebSocket connections to make two-way communication.
https://developer.android.com/reference/java/net/ServerSocket
Check KSWEB and Bit Web Server,
both using Lighttpd als server, php and support mysql.
i tried KSWEB, it works wonderfull, but i don't know yet how to use the modrewrite.
but, it should be work. you can try the trial version of KSWEB before purchase it.
KSWEB costs a little bit more than Bit Web Server only 0,XX $.
if you decided to buy it, post your experience here... i want to know too... :)
Bit Web Server
KSWEB
Till now I have seen all those applications are paid . And others doesn't have phpmyadmin. And I found finally one. You can have a look at kickweb server.
Android web server PHP/MySQL/PHPMyAdmin
Requirements
Internal memory should not be less than 50MB!
Minimum Android API 9 (GINGERBREAD)!
Features
Lighttpd 1.4.34
PHP 5.5.9
MySQL 5.1.62
MSMTP 1.4.31
phpMyAdmin 4.1.10
Nginx 1.5.11
Default Document Root (htdocs)
Path : /sdcard/htdocs/
Default URL
Address : localhost:8080
phpMyAdmin Informations
Address : localhost:10000
Username : root
Password :
MySQL Informations
Host : localhost
Port : 3306
Username : root
Password :
If you find trouble to make it working, you can see this video : https://youtu.be/3_m3vNGTp74
Yes, it is possible. May be not so complicated as Apache, but possible.
Look at Moto Phone Portal for example, it has web-server in its base. And there were several applications for this purpose in Market.
Servers Ultimate Pro, just 8 pounds.
Include PHPAdmin, MySQL and ton of other servers. I am currently using it as proxy.
it is my second time asking a question on stackoverflow. I'm trying to access to my local apache server that I've set up on my desktop computer (ip that starts with ie. 192.168.1*.**)on my android phone. but somehow it does not allow me to access to the server at all! :( strangely, sometimes it does allow me to access to the server lol! but it's only one out of 20 for each attempts. Also, it doesn't allow me to access to the server on my laptop either.. I really need some help guys.. it was working fine when i was working locally within my desktop pc by using emulator. I've done quite a lot of research about it but i wasn't able to find any solutions.
Thank you guys
=====================================
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
HttpClient client = new DefaultHttpClient(setHttpParams());
HttpPost request = new HttpPost(PATH + FILENAME);
if (json != null) {
request.setEntity(new ByteArrayEntity(json.toString().getBytes("UTF8")));
request.setHeader("json", json.toString());
} else {
request.setEntity(new UrlEncodedFormEntity(nameValuePairs));
}
HttpResponse response = client.execute(request);
so that is the code, error occurs when it tries to execute the request. request includes the apache server ip PATH (192.168.**) + FILE_NAME(ie. index.php). I know why it causes the error because i cannot access to the 192.168.* server and read the desired file. I need help with this =(
For the computer running the webserver:
Make sure that your personal firewall rules are not preventing access to the web server.
For your phone to access the webserver over the air the server needs to be accessible from the web. It could be that your machine is behind a corporate firewall which is preventing access to it from external sources.
If you simply want to access it from the phone, make sure that both the webserver and phone are on the same network i.e. they have IP addresses from the same address range. You can do that by connecting your machine to wireless AP (either via ethernet or wireless) and making sure that your phone is connected to the same wireless AP.
Moreover I just read your question again and noticed the line
it does not allow access to to the server on my laptop either
If you cannot access the webserver locally on the machine it is runnning on then there is something wrong in the way you configured your web server.
So first make sure that your web server is running and configured properly. Open a browser and go to http://localhost or http://192.168.x.x if everything is configured right you will be able to access your web pages.
Basically this is networking 101, if it is still giving you problem post some details such as what webserver, what port etc.
If you are new to networking concepts and don't know how to configure a web server say Apache just Google how to configure Apache or use something called XAMPP which is a developer friendly bundle containing Apache, MySQL, PHP, FileZilla etc. Note that XAMPP shall never be used in a production environment as its default configuration is done in a way to make it easier to install, configure and run above mentioned packages for new users.