OkHttp cannot fetch data from localhost - android

Can anyone please show me the way to fetch data from localhost by OkHttp in Kotlin. When i changed url to https, it worked well
Picture in Logcat for successful one
but when i use http://localhost..., it failed to execute
Picture when failed

EDIT - 8/23/22
It is likely you need to set
android:usesCleartextTraffic="true"
as a flag within the <application> element in the AndroidManifest.xml file.
Old Answer
don't use localhost like this. open command prompt if you are using Windows. type ipconfig and then get Local Ip Address and then use that Ip Address to get data from local host.
same method for Mac. you have to get local ip address

Related

java.net.UnknownHostException: Unable to resolve host [LOCAL]

I use the INTERNET uses-permission in the manifest file.
If I type URL url = new URL('http://www.example.com/page') (remote server) it will work.
But if I type URL url = new URL('http://example.local/page') (local server) it will return the error
java.net.UnknownHostException: Unable to resolve host "example.local": No address associated with hostname
Now in %WINDIR%/System32/drivers/etc/hosts I wrote this entry :
10.101.0.179 example.local
I can't use new URL('http://10.101.0.179/page') because example.local is a virtualHost in my apache configuration.
Can't my AVD looks into the hosts file ? what should I do to tell android to fetch the ip 10.101.0.179 but still send a proper information to apache and tell it to serve the virtual host example.local ?
If you try to request a web-page from your local HTTP server and the page is bounded to a virtualHost, so let say you need to reach http://example.local/mypage and if as pointed by #CommonsWare you don't use mDNS to resolve inter-local domain-names, android will probably throw the same error message as it reads in the title of this question. Here's the solution I am using :
let's say http://example.local/mypage refers to the ip address 10.101.0.179, Android won't be able to resolve example.local, you need to write the raw url (of your actual local server)
try {
...
URL url = new URL("http://10.101.0.179/mypage");
HttpUrlConnection conn = (HttpUrlConnection) url.openConnection();
...
}
...
But now when Apache receives this request, unless serving your desired host, 10.101.0.179 won't be understood in case of a VirtualHost. To round-about this problem, just modify the Host http-header property using setRequestProperty :
...
conn.setRequestProperty("Host", "example.local");
...
And it should work.
First you must check if you have given INTERNET permission in your manifest file or not, second you make sure that your emulator or computer has a valid internet. If it doesn't work than you can do following:
If you are using localhost url than you must convert that url into IP address to work with App specially using Android emulator.
For example :
My Current url is
http://mylocaladdress:9090/myapp.svc/ForgetPassword?EmailID=test#test.com
Now you can put the following command into terminal on mac or you can use cmd on windows
ping mylocaladdress
The result will give u the IP address and my final url becomes
http://10.1.2.111:9090/myapp.svc/ForgetPassword?EmailID=test#test.com
This url worked perfectly in android emulator without exception.

Posting Data To Sub-Domain URL on Localhost

Hello I am building my first android app, I am currently trying to post data to a URL on my local server. When I try with my live server it works however with my local server it does not, nothing happens at all, I get no response.
This is the route in my app on my localhost: http://admin.website.dev
I changed my hosts file and virtual host configuration on my localhost so that any address with the extension ".dev" points to a folder on my localhost. eg. "localhost/website"
Is there a reason this does not work in my android app when I try posting data?
I have already added the INTERNET permission in my manifest file.
Since android 5, you need to make a POST on DNS valid URL.
I'm not sure if your changes on hosts file will work.

Phone can't connect to PC to execute PHP file

I created a database in my PC, and now I want to retrieve all table data and display them on my phone usin Google gson library by executing a php file in the server. The problem is the phone can't connect to the PC to execute that php file:
Error 403 for URL http://192.168.1.2/android_connect/get_all_products.php
I'm sure that the code is working cause i tested with that online link and data are retrieved in the phone.
The url is like that in JAVA code:
String url = "http://192.168.1.2/android_connect/get_all_products.php";
Is that correct? Is there something to do to let the phone connect to the PC?
Thank you for helping.
You need to tune your PC's configuration to allow remote requests. Please see what error 403 is. It'd also help to check what's in your HTTPD logs concerning this request.
To allow remote access to apache on Windows change the file: c:\wamp\bin\apache\Apache2.2.22\conf\httpd.conf
You need to allow remote access to directory C:\wamp\www like this:(add that lines)
<Directory "C:/wamp/www/">
Order Deny,Allow
Allow from all
</Directory>
And RESTART YOUR SERVER.

Android emulator cannot connect with local tomcat server with HttpURLConeectioin

I'm doing a project to connect Android with local Tomcat server. But now I'm facing a problem for about 1 week. Who can help me..Thanks very much!
I use the newest android API and Tomcat 7.0. When I start Tomcat server. I can access the Tomcat homepage through emulator browser with url:(http://10.0.2.2:8080). But when I use HttpUrlConnection in the code. I cannot get the successful connection. I also use httpurlconnection to access www.google.com and www.android.com. I cannot connect them either.I use HttpURLConnection.HTTP_OK to see whether it make a successful connection. I also see the Connected is false for HttpURLConnection instance I make.
By the way I also add the internet access permission for it in AndroidManifest.xml before tag.
Thank you very much! Help me. It's emergency.
I recently had this problem. As mentioned above, (on Windows) start cmd, and use ipconfig to find the PC's IP address. Also, due to my Eclipse/Tomcat configuration I needed to have my Eclipse project name in the URL. The URL I used was like this: http://192.168.1.2/myEclipseProject/ServletName
Also, you may need to add the port to the ip address, if your Tomcat still has the default configuration: 192.168.1.2:8080

SmartFoxServer unable to connect to server

I am working in android chat application.
I used SmartFox server. I am using 10.0.2.2 as server IP address and 9933 as Port.
Whenever i run this application using emulator then works fine but when i run this application in my real device then connection does not performed correctly. I think this is unable to find out IP 10.0.2.2.
I changed this IP address to 192.168.1.9 which is IP address of my system in LAN on which SmartFox server is installed.
Please suggest me what mistake i have done.
Thank you in advance.
I resolved my problem myself. I created one entry in confi/server.xml file of server.
like this:-
<serverSettings>
<socketAddresses>
<socket address="192.168.1.9" port="22" type="TCP"/>
<socket address="192.168.1.9" port="22" type="UDP"/>
</serverSettings>
</socketAddresses>
or you may add this entry using admin module of Smartfox server. This task can be done using Server configuration part.

Categories

Resources