Rest Api Link build - android

I have made a rest api with database for an Android app. I have made this in php in my localhost. Now I want to access it from my Android app. How can I access it through URL in internet. I am totally new to this, plz give me in details.

This question, brings back so much memories. You need to tunnel your local host packets to the public internet. There are many tools, but the main one i use is NgRok.
Details
Test mobile apps against a development backend running on your machine. Point ngrok at your local dev server and then configure your app to use the ngrok URL. It won't change, even when you change networks.
Don’t constantly redeploy your in-progress work to get feedback from clients. ngrok creates a secure public URL (https://yourapp.ngrok.io) to a local webserver on your machine. Iterate quickly with immediate feedback without interrupting flow.
A note of interest. if your app currently as a https signed certificate you might need to add some extra classes, if not you good to go. Read up here.
Guide lines for local public hosting.
Create your webserver(php,node.js etc) , start it, and verify on localhost:port
Start ngrok, its simple visit the website, the instructions are there
Ngrok will give you a url, that url is public anyone can access it, while your computer is powered on
On android side, use a http client, such as Volley, Http2 or Retrofit.
You have a few reading up to do

Related

How to connect an android app running on a device connected via ADB to hit REST API on local server?

I have a multi-tenant web app like
http://xxxxx.constant-domain.com:8080/
in my host file
127.0.0.1 xxxxx.constant-domain.com
The device that has the android app running is connected via ADB which calls my webapp's Rest Apis to get and send data and
It runs fine on dev and prod environments like
http://xxxxx.dev.constant-domain.com/get-data
but the problem is I want to debug it on my local server. I tried adding the given domain as base url but it doesn't work.
I tried adding http://localhost:8080/ as well but it doesn't work.
I tried looking for questions here but didn't find anything that would help.
I'm using retrofit for Api calls.
Any help would be appreciated.
First of all, make sure that your local server and your device are using a same wifi. And then find out the option to put your local server online. There is an option in Wamp Server to put it online. I don't know which software you are using to setup apache or any kind of server on your computer.
And then you will be able to access your local server by ip address. Also there is an option to edit virtual hosts of your server to allocate a specific port that the app can connect.
It doesn't matter whether you use Retrofit or Volley or Okhttp or any other things. You need to setup your local server to be online within your local network.
I figured it out. All I needed to do was to set my private Ip as
http://192.168.8.400:8080/get-data
Hope this helps anyone who is in this situation.

How to make local area network HTTP request from Google Assistant?

I want to make HTTP requests to a server (such as Node-red or Arduino) on my LAN directly from my Google Assistant.
I would like to reproduce the behavior of this widget which, however, does not work from google assistant.
Using the IFTTT with webhooks makes the IFTTT website to ping on the internet from their server, not from my mobile in my private LAN. I think same will happen if I use google actions with api.ai with webhook. For example, this app, Wake On Lan can LAN requests, but it won't take links like 192.168.0.2:1880/test, it only takes IP addresses, but it works with the Google Hot word, OK Google.
So how can I make HTTP POST requests directly from my Google Assistant?
I'm not 100% sure what you are looking to do here, but I THINK ngrok might be able to help you.
If your goal is to be able to access a server that is only accessible within your local network, then what you really need is a way to make that server accessible from outside the network, but only to you. You have two ways of doing this. One is to open up a port on your router, which is inherently unsafe. The other is to use ngrok. ngrok is an application that you run on your local server which opens a secure tunnel into your local network. You access it using a url externally that looks like this:
https://02355cab0.ngrok.io/
The hash is unique and only you know it. That URL is accessible from anywhere on the internet, but points to a server on your local LAN, ie:
https://02355cab0.ngrok.io/ -> http://192.168.0.2:1880
Check it out here:
https://ngrok.com/
Using this ngrok url then, you can give that to the Google Assistant or whatever app (IFTTT if you want), to make calls from the web directly to your local network.

android - Connect to mySQL database over LAN without using a webserver

I want to have multiple clients that connect to a server over LAN and access/modify the mySQL database in the server.
How would i go about doing this? Can you guys provide some resources/links that i could research on the topic
To answer your question, you should be able to connect to a mysql database by adding the jdbc driver to your project as a jar file in Android Studio.
Now for a real app that you plan to distributed to thousands of users there are Security issues, Performance issues, and Scalability issues.
Security issues:
You expose your database directly to the internet by opening its port to public access for the apps to connect. A web app adds a layer in the middle, keeping the database access inside the intranet.
You expose your data directy to the public by providing at least one public account known by everybody (I assume this would be the way to access because managing one account per user wouldn't be realistic). A Web app isolates the user account from the database accounts.
By providing access this way, as android mobile devices can be rooted, you are potentially granting anonymous access to your data.
Performance Issues:
With a web app in the middle, it is the webapp who manages the
connections to the database. This enables sharing connections
amongst different users vs. one dedicated connection per user would
have if the different devices estable separate connections.
For the same reason, you can't take advantage of connection pooling,
which saves the overhed of establishing a connection to the database
for each incoming request.
Scalability issues:
As connections are not shared the number of concurrent users will be bound to the number of connections you can open at the same time to the database.
EDIT 1
I am adding an alternative I thought of which involves using a web application but it is not implemented using a webserver. It is a java NIO framework that runs on its own. The limitations of this solution is you need shell access to the server and java, which is not common in traditional hostings. Checkout Netty.
There are 2 ways how to do perform your task. You can either add the JDBC driver in android studio, or better implement a REST API that connects to your database, and all the android clients can send HTTP requests to the server and the server will add the information for you. Here you can implement the create, update, delete methods. For HTTP requests you can use Retrofit or Volley libraries.
If you want to use JDBC, check out the answer here How to Mysql JDBC Driver to android studio
But the best and most correct solution for this type of problems would be a REST Service
In the long run, you really need a "client" application between "users" and the database. It is usually done via a webserver, plus PHP/Java/VB/.... Yes, it requires you learn yet another language, but that is not something to avoid in a serious application.
The client can help (and hurt, if done adequately) with security. The client can insulate users from database changes, which will eventually happen. The client should 'abstract' the interface to the DB so that the users do not have to be SQL-savvy. Etc.
You have might installed WAMP Server / XAMPP Server for mySQL Database
Click on WAMP icon and select Apache, Open "httpd.conf" and find tag starts with
<Directory "c:/wamp/www">
...
</Directory>
and update the code as below
<Directory "c:/wamp/www">
AllowOverride All
Require all granted
Order Deny,Allow
Deny from none
Allow from all
Allow from 127.0.0.1
Allow from ::1
Allow from localhost
</Directory>
Now Create REST APIs in your local server as you might use PHP or whatever,
and connect android app with apis with assigned IP Address to your computer in LAN.
It is not one single answer, but a group of answers. Firstly what you need is the concept of data forwarding through introspected tunnels over the network. At the end of the day, your database is always listening on certain port, that is local to your machine, meaning only you can access and modify the contents of the database. For example if you access PHPMyAdmin, you can go the MySQL address on your local machine.
What you need to do is make that access public to the internet, what you need is to broadcast your existence(internet protocol address) to the web. Thus making a public hub, in short, Local-tunnels allows you to easily share a web service on your local development machine without messing with DNS and firewall settings.
By tunneling your local machine to web , anyone with the assigned IP address can access your machine(database) over any connection,not just LAN or WI-FI. There are many options to choose from, There is ngrok,which exposes a local server behind a NAT or firewall to the internet.
Features are,
Don’t constantly redeploy your in-progress work to get feedback from clients. ngrok creates a secure public URL (https://yourapp.ngrok.io) to a local webserver on your machine. Iterate quickly with immediate feedback without interrupting flow.
Test mobile apps against a development backend running on your machine. Point ngrok at your local dev server and then configure your app to use the ngrok URL. It won't change, even when you change networks.
Building web hook integrations can be a pain: it requires a public address and a lot of set up to trigger hooks. Save yourself time and frustration with ngrok. Inspect the HTTP traffic flowing over your tunnel. Then, replay web-hook requests with one click to iterate quickly while staying in context.
Own your data. Host personal cloud services on your own private network. Run web-mail, file syncing, and more securely on your hardware with full end-to-end encryption.
Its really great, however there is a side note, because this procedure opens up your local machine and renders it IP accessible on the internet, many different security challenges arise, so it is only recommended for testing purposes with none sensitive data.
Hope this helps:)

Building an Apprtc for native android with nodejs server

I've successfully build the AppRTC for native android by taking code from https://github.com/njovy/AppRTCDemo
and I'm able to make videocalls providing the address of the demo app ( https://apprtc.appspot.com/?r=XXXXXXXX ). Now I have install the nodejs
and also run the node static server in my system. I also know that to run the app on my local server we have remove Google TURN server. But how to bypass this turn server. I have also read this ApprtcDemo with local server works between browsers but not Android native to browser but can't able to understand clearly.
The problem is that what I need to make change in my code and how to install turn server for node server so that android can connect to my local server. How to manage the setup of turn server in Centos7 and what is apprtc.py
I am new to this and does not have much knowledge of server so if i have asked anything wrong so ignore it but please reply me.
After lots of effort i have install the turn server by following the link https://www.webrtc-experiment.com/docs/TURN-server-installation-guide.html
Now i have to change the url in my code but i can't able figure what and where to make change in Apprtc demo sample for native android
nodejs has nothing to do with TURN server, I believe you are using nodejs for signalling. If you do not want to use TURN server then you have to remove this url from iceServer configuration. For TURN server local setup, what TURN server you are trying to setup locally? May be you should find some free TURN serve binary along with setup documentation. But why don't you just try any TURN service available for free or as an evaluation?
[Edited] The purpose of TURN server is to facilitate connectivity when both end point are not able to connect using the local candidates where STUN server helps to identify the server-reflexive candidate and TURN is to allocate a Relay candidate. Just to understand your case, you want to run TURN/STUN server in your LAN setup? then there is no use of TURN I guess as TURN server itself is residing inside of the NAT. The general setup requirement of TURN server is have a public IP address which will be used as a Relay candidate for the end points. If you understand this case and still want to setup TURN inside of NAT then its not a harm but to remind you that you won't be able to use TURN when any end point is our of that LAN.
After lots of searching on google i have find out my solution from https://github.com/webrtc/apprtc link here you have to use google app engine along node js for making local server.

Setting up a development server for mobile applications [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
How does one set up a development server for mobile apps?
In web development developers usually install XAMPP and then use localhost to communicate with the server and test in a safe environment. This is a free and fast working solution, since it requires very little or no upload time. What is an equivalent for mobile developers on the iOS or Android platform? Some kind of localhost for mobile app developers.
The goal is to develop a backend for a mobile app in a safe and quick environment. Their exact equivalent for web developers is using localhost.
Example: The user registers. Username and password along with an image are sent to the server and stored in the database. The next time the user logs in the server looks up in the database and serves the correct image to the user and logs him/her in.
I am not asking for how to program this or what code to use. I am asking for instructions to a set up developer test environment - a localhost for mobile apps, if such a thing exists.
As you mentioned, web developers, can quickly set your localhost by installing XAMPP or any equivalent web server, a database server like MySQL on localhost. They put their web services on a web server and call or use them from the front end (HTML page) on user actions like form submissions, button clicks, etc.
Mobile application development with Android is very much similar to the above approach. All web services lie at the back end, and you only call them from an Android client (mobile device) using the HttpClient or HttpUrlConnection class.
You can make only HTTP requests from a mobile application and get a response from the services which are called, parse the response and populate the UI elements.
You can set up your localhost with a database server, a web server as you mentioned correctly above. Now to make a request from your mobile device to localhost, keep your device and localhost machine on the same network (you can put both devices on the same Wi-Fi network) so that you can directly make requests to your localhost from your mobile application. You will get a response in the mobile app in whichever format you have provided and use it. This is very similar to that of web application, where you can imagine your UI lying in mobile device which was an HTML page in a web application.
Assuming that you have a web server / server application installed on your machine, then the requirement is that the device and server can communicate.
This is normally done using the HttpURLConnection class. You will need the IP address of your server in order to construct your URL, that is instead of accessing your server application using localhost/myapp you will need to add the url 192.168.0.255/my app (or whatever your actual IP address is).
There are a couple of issues to watch out for. Your development machine must accept incoming http requests (port 80 normally) through the firewall, and your router must allow the traffic between the machine and the device. Have a look at http://portforward.com/ if you are having trouble with this. If you are having trouble diagnosing the issues, then sometimes downloading a terminal app on the device can help.
Sometimes cheap routers or corporate policies make this difficult. In this case you can try setting up a portable hotspot or creating an ad-hoc network if your server machine also has a wireless network adapter.
If you're developing a mobile app then I would think you want to keep focussed on the mobile development part and therefore mock out the backend rather than write a working server with a database, logic, etc.
In very simple cases I would recommend using something such as Charles. This allows you to intercept requests in and out of your mobile app either from the simulator or device and validate everything is working as expected at a call by call level. If you want something more persistent then using a simple web framework in your language of choice with fixed responses would do the job well. For example, Sinatra for Ruby or Express.js for Node.js. And then rather than worrying about having a database to maintain and seed, just return fixed JSON responses for a particular call or a file from disk. This could then be committed with your mobile development code repositories and used as part of test suites.

Categories

Resources