how to change port 80 android - android

I made a webserver on my android device(using Nanohttpd). It's working fine over the port 8080
but I want to make it okay over the port 80,(I want to tape on my browser : http://192.168.x.x instead of http://192.168.x.x:8080/ ) but I'm not able to do it
This is how I call the method that creates the webserver:
httpServer = new NanoHTTPD(80,Environment.getExternalStorageDirectory());
Can someone tell me what service is running by default over the port 80 ?
How can i fix this problem ?

I finally found a way to fix that, for those who have the same problem, here is the solution
I rooted the phone using UnlockRoot
then I installed Port redirector from the play store and I forwarded the traffic from port 80 to port 8080, and now my web server is available from the address: 192.168.x.x
Hope it will help :)

Ports below 1024 are restricted on Unix like systems. You need superuser privileges to bind to these "well-known" ports.
List of these "well-known" ports on wikipedia

Check out line 89 of the server source:
https://github.com/NanoHttpd/nanohttpd/blob/master/core/src/main/java/fi/iki/elonen/NanoHTTPD.java
I don't even see a constructor that type-matches your arguments. That might be part of the problem.

Related

Call localhost API within react native ( android device : connected through local)

Have created a simple GET API using node.js and trying to consume it within my expo-react native project using axios/fetch.
The GET API is called whenever user clicks on submit button.
Submit -> http://localhost:port/api
However, when I am trying to test the feature on my android device by connecting the device through datacable and selecting "local" connect type in expo, the API call always falls under exception.
When I tried to log the exception, the exception is printed empty.
I double checked the API and it works fine when tested through postman.
I tried replacing localhost url with my-Ip (http://IP:port/api) and also with 127.0.0.1/127.0.2.2 (http://127.0.0.1:port/api , http://127.0.2.2:port/api) , but nothing seems to make the API call a success.
When I replaced the localhost URL with Facebook's network sample API, the application worked just fine. So, there is no issue with the API calling method.
Also, tried adding the port to windows inbound firewall request but that didn't work too.
Can someone help me out with this ?
You must call the API from your local IP address. To get your local IP, just do the following:
If you are on Ubuntu 16.04:
ifconfig | grep 192
If you are on Ubuntu 18.04:
ip -c a | grep 192
Your local IP address will probably be something like 192.168.0.XXX.
yes, i also had this problem until use my Local IP (example: http://192.168.1.55:port) instead 127.0.0.1
please see this answer
Good day,
This is working
I use my machine's IP the Ethernet adapter Ethernet 2: IPV4 Address
And allow my port on firewall both Inbound and outbound rules

How do I resolve a Bonjour domain name on Android?

I need to get my app to play a video file located on my network. I know the url of the file is:
http://something.local/abc.mp4
Now, when I manually substitute "something.local" with its true ip address, the MediaPlayer has no problem playing it. Nonetheless, when I have the above address, the MediaPlayer errors out with error (1, -1007).
So I'm assuming this is because Android doesn't understand "something.local" as being correct.
My question is: How can I "translate" something.local into an ip myself, so that I can then pass it into MediaPlayer?
A small caveat: I believe that MediaPlayer does not work with IPv6 addresses, so please keep that in mind...
Just a side note, in case it makes my situation clearer: When I run ping something.local -4 in the Windows command prompt, it returns:
Pinging something.local [192.168.1.126] with 32 bytes of data:
Reply from 192.168.1.126: bytes=32 time=145ms TTL=64
Reply from 192.168.1.126: bytes=32 time=112ms TTL=64
Reply from 192.168.1.126: bytes=32 time=32ms TTL=64
Reply from 192.168.1.126: bytes=32 time=169ms TTL=64
That translation where windows went from something.local -> 192.168.1.126 is what I want to do in my Android app.
Firstly, you need read document about Bonjour (iOS term) or Zero Config (Linux term).
To understand what's something.local:
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/NetServices/Articles/about.html#//apple_ref/doc/uid/TP40002458-SW1
For example, if a user types steve.local. into a Web browser, this
tells the system to multicast the request for steve on the local
network instead of sending it to the conventional DNS server. If a
Bonjour-enabled computer named steve is on the local network, the
user’s browser is sent the correct IP address for it. This allows
users to access local hosts and services without a conventional DNS
server.
For how to resolve it:
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/NetServices/Articles/NetServicesArchitecture.html#//apple_ref/doc/uid/20001074-SW1
For java library, previous answers provided good enough example.
You should try this snippet with jmDNS library api.. may need some changes.
JmDNS jmdns = JmDNS.create();
DNSEntry addressEntry = jmdns.getCache().getDNSEntry(name, DNSRecordType.TYPE_A, DNSRecordClass.CLASS_ANY);
if (addressEntry instanceof DNSRecord) {
ServiceInfo cachedAddressInfo = ((DNSRecord) addressEntry).getServiceInfo(true);
if (cachedAddressInfo != null) {
for (Inet4Address address : cachedAddressInfo.getInet4Addresses()) {
// use the `address`
}
}
You have access to java,net APIS on android and can use them to resolve adresses.
http://docs.oracle.com/javase/1.4.2/docs/api/java/net/InetAddress.html
However, success will depend on network proper configuration. Your device receives DNS server setup via DHCP - so you are at mercy of network provider

Accessing localhost:port from Android emulator

I'm running a web service on my local machine that runs at localhost:54722.
I want to call the service from an app running in the Android emulator.
I read that using 10.0.2.2 in the app would access localhost, but it doesn't seem to work with the port number as well. It says HttpResponseException: Bad Request.
You can access your host machine with the IP address "10.0.2.2".
This has been designed in this way by the Android team. So your webserver can perfectly run at localhost and from your Android app you can access it via "http://10.0.2.2:<hostport>".
If your emulator must access the internet through a proxy server, you can configure a custom HTTP proxy from the emulator's Extended controls screen. With the emulator open, click More , and then click Settings and Proxy. From here, you can define your own HTTP proxy settings.
Use 10.0.2.2 for default AVD and 10.0.3.2 for Genymotion
Since 10.0.2.2 is not a secure domain for Android you have to allow non-secured domains in your network configuration for API 28+ where non-TLS connections are prevented by default.
You may use my following configurations:
Create a new file in main/res/xml/network_security_config.xml as:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">localhost</domain>
<domain includeSubdomains="true">10.0.2.2</domain>
</domain-config>
</network-security-config>
And point it in AndroidManifest.xml
<application
......
......
android:networkSecurityConfig="#xml/network_security_config">
I faced the same issue on Visual Studio executing an web app on IIS Express. to fix it you need to go to your project properties then click on Debug Tab and change http://localhost:[YOUR PORT] to http://127.0.0.1:[YOUR PORT] and set the android url to http://10.0.2.2:[YOUR PORT]. it worked for me.
I'm not sure this solution will work for every Android Emulator and every Android SDK version out there but running the following did the trick for me.
adb reverse tcp:54722 tcp:54722
You'll need to have your emulator up an running and then you'll be able to hit localhost:54722 inside the running emulator device successfully.
If you are using IIS Express you may need to bind to all hostnames instead of just `localhost'. Check this fine answer:
https://stackoverflow.com/a/15809698/383761
Tell IIS Express itself to bind to all ip addresses and hostnames. In your .config file (typically %userprofile%\My
Documents\IISExpress\config\applicationhost.config, or
$(solutionDir).vs\config\applicationhost.config for Visual Studio
2015), find your site's binding element, and add
<binding protocol="http" bindingInformation="*:8080:*" />
Make sure to add it as a second binding instead of modifying the existing one or VS will just re-add a new site appended with a (1) Also, you may need to run VS as an administrator.
I solved it with the installation of "Conveyor by Keyoti" in Visual Studio Professional 2015.
Conveyor generate a REMOTE address (your IP) with a port (45455) that enable external request.
Example:
Conveyor allows you test web applications from from external tablets and phones on your network or from Android emulators (without http://10.0.2.2:<hostport>)
The steps are in the following link :
https://marketplace.visualstudio.com/items?itemName=vs-publisher-1448185.ConveyorbyKeyoti
The problem is that the Android emulator maps 10.0.2.2 to 127.0.0.1, not to localhost. So configure your web server to serveron 127.0.0.1:54722 and not localhost:54722. That should do it.
After running your local host you get http://localhost:[port number]/ here you found your port number.
Then get your IP address from Command, Open your windows command and type ipconfig
In my case, IP was 192.168.10.33 so my URL will be http://192.168.10.33:[port number]/.
In Android, the studio uses this URL as your URL. And after that set your URL and your port number in manual proxy for the emulator.
I have a webserver running on my localhost.
If I open up the emulator and want to connect to my localhost I am using 192.168.x.x. This means you should use your local lan ip address. By the way, your HttpResponseException (Bad Request) doesn't mean that the host is not reachable.
Some other errors lead to this exception.
To access localhost on Android Emulator
Add the internet permission from AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
Add android:usesCleartextTraffic="true", more details here:
Run the below-mentioned command to find your system IP address:
ifconfig | grep "inet " | grep -v 127.0.0.1
Copy the IP address obtained from this step (A)
Run your backend application, which you can access at localhost or 127.0.0.1 from your sytem.
Now in android studio, you can replace the URL if you're using in code or You can use the ip address obtained from step(A) and try opening in web browser,
Like this http://192.168.0.102:8080/
Don't forget to add PORT after the IP address, in my case app was running on 8080 port so I added IP obtained in (A) with the port 8080
you need to set URL as 10.0.2.2:portNr
portNr = the given port by ASP.NET Development Server my current service is running on localhost:3229/Service.svc
so my url is 10.0.2.2:3229
i'd fixed my problem this way
i hope it helps...
"BadRequest" is an error which usually got send by the server itself, see rfc 2616
10.4.1 400 Bad Request
The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repeat the request without modifications.
So you got a working connection to the server, but your request doesn't fit the expecet form. I don't know how you create the connection, what headers are included (if there are any) – but thats what you should checking for.
If you need more help about, explain what your code is about and what it uses to connect to the Server, so we have the big picture.
Here is a question with the same Problem – the answer was that the content-type wasnt set in the header.
1) Run ipconfig command in cmd
2) You will get result like this
3) Then use IPv4 Address of VMWare Network Adapter 1 followed by port number
In My Case its 8080, so instead of using localhost:8080
I am using 192.168.56.1:8080
Done.....
I would like to show you the way I access IISExpress Web APIs from my Android Emulator. I'm using Visual Studio 2015. And I call the Android Emulator from Android Studio.
All of what I need to do is adding the following line to the binding configuration in my applicationhost.config file
<binding protocol="http" bindingInformation="*:<your-port>:" />
Then I check and use the IP4 Address to access my API from Android emulator
Requirement: you must run Visual Studio as Administrator. This post gives a perfect way to do this.
For more details, please visit my post on github
Hope this helps.
For Laravel Homestead Users:
If anyone using Laravel with homestead you can access app backend using 192.168.10.10 in emulator
Still not working?
Another good solution is to use ngrok https://ngrok.com/
I am using Windows 10 as my development platform, accessing 10.0.2.2:port in my emulator is not working as expected, and the same result for other solutions in this question as well.
After several hours of digging, I found that if you add -writable-system argument to the emulator startup command, things will just work.
You have to start an emulator via command line like below:
emulator.exe -avd <emulator_name> -writable-system
Then in your emulator, you can access your API service running on host machine, using LAN IP address and binding port:
http://192.168.1.2:<port>
Hope this helps you out.
About start emulator from command line: https://developer.android.com/studio/run/emulator-commandline.
Explanation why localhost is not available from emulators for anyone who has basic access problem. For sophisticated cases read other answers.
Problem: Emulator has own local network and localhost maps itself to emulator, but NOT your host!
Solution:
Bind your server to 0.0.0.0 to make it available for emulator's network
Get external IP address of your laptop: ifconfig command for Mac
In Android (or Flutter app) use IP address of your external interface like: 192.168.1.10 instead of localhost
I had the same issue when I was trying to connect to my IIS .NET Webservice from the Android emulator.
install npm install -g iisexpress-proxy
iisexpress-proxy 53990 to 9000 to proxy IIS express port to 9000 and access port 9000 from emulator like "http://10.0.2.2:9000"
the reason seems to be by default, IIS Express doesn't allow connections from network
https://forums.asp.net/t/2125232.aspx?Bad+Request+Invalid+Hostname+when+accessing+localhost+Web+API+or+Web+App+from+across+LAN
localhost seemed to be working fine in my emulator at start and then i started getting connection refused exception
i used 127.0.2.2 from the emulator browser and it worked and when i used this in my android app in emulator it again started showing the connection refused problem.
then i did ifconfig and i used the ip 192.168.2.2 and it worked perfectly
Bad request generally means the format of the data you are sending is incorrect. May be mismatched data mapping . If you are getting bad request implies you are able to connect to the server, but the request is not being sent properly.
If anybody is still looking for this, this is how it worked for me.
You need to find the IP of your machine with respect to the device/emulator you are connected. For Emulators on of the way is by following below steps;
Go to VM Virtual box -> select connected device in the list.
Select Settings ->Network-> Find out to which network the device is attached. For me it was 'VirtualBox Host-Only Ethernet Adapter #2'.
In virtualbox go to Files->Preferences->Network->Host-Only Networks, and find out the IPv4 for the network specified in above step. (By Hovering you will get the info)
Provide this IP to access the localhost from emulator. The Port is same as you have provided while running/publishing your services.
Note #1 : Make sure you have taken care of firewalls and inbound rules.
Note #2 : Please check this IP after you restart your machine. For some reason, even If I provided "Use the following IP" The Host-Only IP got changed.
I resolved exact the problem when the service layer is using Visual Studio IIS Express. Just point to 10.0.2.2:port wont work. Instead of messing around the IIS Express as mentioned by other posts, I just put a proxy in front of the IIS Express. For example, apache or nginx. The nginx.conf will look like
# Mobile API
server {
listen 8090;
server_name default_server;
location / {
proxy_pass http://localhost:54722;
}
}
Then the android needs to points to my IP address as 192.168.x.x:8090
if you are using some 3rd party package like node express or angular-cli you will need to find the IP of your machine, and attach your host to that IP within the server startup config (instead of localhost). Then launch it from the emulator using the IP. For example, I had to use: ng serve -H 10.149.212.104 to use the angular-cli. Then from the emulator I used: http://10.149.212.104:4200
If you are working with Asp.Net Web API, in .vs/config folder inside your project, modify these lines as per you port setting. Let suppose you use port 1234 and physicalPath to the project folder set by IIS is "D:\My Projects\YourSiteName", then
<site name="YourSiteName" id="1">
<application path="/" applicationPool="Clr4IntegratedAppPool">
<virtualDirectory path="/" physicalPath="D:\My Projects\YourSiteName" />
</application>
<bindings>
<binding protocol="http" bindingInformation="*:1234:*" />
</bindings>
</site>
In android studio, access your api with "http://10.0.2.2:1234" ...

JCIFS, Android, and getbyname

Well, tried the following bit of code:
import jcifs.netbios.NbtAddress;
...
try{
NbtAddress addr = NbtAddress.getByName("Base");
}catch (UnknownHostException e){}
...
And it throws that exception.
Any ideas? I have tried it with a couple of named machines on the LAN
I guess you don't have the INTERNET permission in your manifest?
I was using the emulator.
When I ran the app on real hardware the problem disappeared.
The problem is that NBT name resolution works by sending UDP broadcast packets on the LAN, and the emulator lives on its own virtual LAN, connected to your host PC only by a virtual NAT router. Broadcast packets don't cross between different subnets, so nothing on your LAN sees the name query.
I have the same problem. The only ideas for solving it that I know are:
Set up a WINS server on your LAN, and point your app at it with the jcifs.netbios.wins property,
or,
Create an LMHOSTS file in the emulator's filesystem, containing the names and IPs of the hosts on your LAN that you want to be able to reach, and point your app at it with the jcifs.netbios.lmhosts property.
If it was possible to run something on the emulator that could serve as a NetBIOS Master Browser, there might be a third solution, but I haven't researched that enough yet.
The LMHOSTS solution isn't ideal, because you don't (AFAIK) get to browse for workgroups and hosts. That should work with a WINS server, but I haven't tried it.

java.net.ConnectException: localhost/127.0.0.1:8080 - Connection refused

I am using xampp apache server to serve resources to the application from my machine. But i am getting the above error.
I got something on the google. pointing towards possible solution here
http://groups.google.com/group/android-beginners/browse_thread/thread/599a06416fb37b4a
What is the solution for the above problem?
Since you have not specified you are connected to a server from the device or emulator so I guess you are using your application in the emulator.
If you are referring your localhost on your system from the Android emulator then you have to use http://10.0.2.2:8080/ Because Android emulator runs in a Virtual Machine therefore here 127.0.0.1 or localhost will be emulator's own loopback address.
Refer: Emulator Networking
in android
Replace: String webServiceUrl = "http://localhost:8080/Service1.asmx"
With : String webServiceUrl = "http://10.0.2.2:8080/Service1.asmx"
Good luck!
localhost and 127.0.0.1 are both ways of saying 'the current machine'. So localhost on your PC is the PC and localhost on the android is the phone. Since your phone isn't running a webserver of course it will refuse the connection.
You need to get the IP address of your machine (use ipconfig on windows to find out) and use that instead of 127.0.0.1. This may still not working depending on how your network/firewalls are set up. But that is a completely different topic.
Add Internet permission in Androidmanifest.xml file
uses-permission android:name="android.permission.INTERNET
Open cmd in windows
type "ipconfig" then press enter
find IPv4 Address. . . . . . . . . . . : 192.168.X.X
use this URL
"http://192.168.X.X:your_virtual_server_port/your_service.php"
You just have to use your local (but real) IP address and port number like this:
String webServiceUrl = "http://192.168.X.X:your_virtual_server_port/your_service.php"
And make sure you did set the internet permission within the manifest
<uses-permission android:name="android.permission.INTERNET" />
If you are using localhost in your url and testing your application in emulator , simply you can replace system's ip address for localhost in the URL.or you can use 10.0.2.2 instead of localhost.
http://localhost/webservice.php to http://10.218.28.19/webservice.php
Where 10.218.28.19 -> System's IP Address.
or
http://localhost/webservice.php to http://10.0.2.2/webservice.php
You just have to use your local IP address:using the cmd command "ipconfig" and your server port number like this:
String webServiceUrl = "http://192.168.X.X:your_local_server_port/your_web_service_name.php"
And make sure you did set the internet permission in your project manifest
It's working perfectly for me
Good Luck
its working for me. I use genymotion for Run App.
1.Firstly i was checked my local ip. goto command mode>> And write ipconfig.
Example: Windows 10>> search cmd>>then Write ipconfig .
2. Then get your local ip information >>>
3.Use give your localhost ip and virtual box ip. You need to use virtual box ip for genymotion.Check below screenshot. You can you below any ip under virtualbox host network
Replacing localhost with 10.0.2.2 is correct, but you can alsor replace localhost with your physical machine's ip(it is better for debug purposes).
Ofc, if ip is provided by dhcp you would have to change it each time...
Good luck!
Solution is very simple.
1 Add Internet permission in Androidmanifest.xml file
<uses-permission android:name="android.permission.INTERNET" />
[2] Change your httpd.config file
Order Deny,Allow
Deny from all
Allow from 127.0.0.1
TO
Order Deny,Allow
Allow from all
Allow from 127.0.0.1
And restart your server.
[3] And most impotent step.
MAKE YOUR NETWORK AS YOUR HOME NETWORK
Go to Control Panel > Network and Internet > Network and Sharing Center
Click on your Network and select HOME NETWORK

Categories

Resources