i get a strange error message when i try to start an url connection.
here´s the log output:
E/admarvelservice( 2366): postString: &site_id=14488&partner_id=ef8a30b841b36346& timeout=5000&version=1.5&language=java&format=android&sdk_version=2.1.3&sdk_version_date=2011-08-08&device_model=Nexus+S&device_name=GRJ22&get_cached_ads=true&device_systemversion=2.3.5&resolution_width=480&max_image_width=480&resolution_height=800&max_image_height=800&device_os=Android&target_params=appv%3D%3ES%7C%7Cla%3D%3Ede%7C%7Cco%3D%3EAT%7C%7Cscreenorient%3D%3Ep%7C%7Cappvn%3D%3E3.4.0%252F%7C%7Cosv%3D%3E2.3.5
D/admarvelservice( 2366): Error: java.net.ConnectException: ads.admarvel.com/127.0.0.1:80 - Connection refused
anybody an idea whats the problem here?
thanks.
EDIT: how i connect:
URL myURL;
try {
myURL = new URL(_url);
m_ucon = myURL.openConnection();
m_ucon.setConnectTimeout(8000);
} catch (Exception e) {
e.printStackTrace();
}
funny thing is that the connection is working, but every second my logcat shouts the errormessage shown above.
I think this has nothing to do with your application.
The exception comes from an Advertising Service (admarvelservice).
You are using an adblocker app and you have Shazam installed. Just either get the paid version of Shazam, remove ad blocker or just ignore the error.
Check this first in your AndroidManifest.xml:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
Localhost ip address is: 127.0.0.1 so u can check by this too.
try with this http connection:
public SomeClass {
HTTTPUrlConnection conn = new HTTPUrlConnection(...);
SomeClass instance = new SomeClass();
private SomeClass() {}
public static getInstance() {
return instance;
}
}
you should try Singleton pattern for request response.
You must set uses permission in the AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
For my experience
Connection Refused
is caused by :
a|- You don´t have access because probably you are under a Proxy and you need credentials to establish a connection.
b|- You need to add the persmission for connection inside your AndroidManifest.xml:
<uses-permission android:name="android.permission.INTERNET" />
Related
Ive been playing with .net Maui and had some success doing Bluetooth LE but for reasons I've gone ahead and changed my implementation to use wifi and a restful API. I am building for both android and IoS. I am testing with a real device, running android 12.
I am trying to make a basic http(s) (tried both http and https) request to a local server (an esp32). My url is:
const string url = "https://192.168.4.1/api";
My manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk android:minSdkVersion="28" android:targetSdkVersion="31" />
<application android:allowBackup="true" android:icon="#mipmap/appicon" android:supportsRtl="true"></application>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
My initialisation function, which gets called when my page is loaded - MainPage()
private void init_network()
{
client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
}
and finally, my send function which gets called by a button pressed event
private bool SendCommand(string command)
{
try
{
var msg = new HttpRequestMessage(HttpMethod.Post, url);
msg.Content = JsonContent.Create(command);
HttpResponseMessage response = client.Send(msg); // line where the code throws exception
if (response.IsSuccessStatusCode)
{
return true;
}
}
catch (Exception err)
{
Console.WriteLine(err.Message);
}
return false;
}
I also have a runtime permissions section which asks for location_fine_access, which I've given access for.
When running through the request function, I get an exception thrown with the message:
[DOTNET] Operation is not supported on this platform.
I thought this was a permissions issue, and so android isn't letting me make the request but now I'm not so sure. Anyone else had and resolved this issue?
Looks as though .net maui doesnt hook into the android http client properly and is an issue on github. https://github.com/dotnet/maui/issues/1260
the suggested work around, which I have tested and works for me, is to instantiate your HttpClient with the native android handler like this as a workaround:
new HttpClient(new Xamarin.Android.Net.AndroidMessageHandler()).
My code is as follow:
public chkRechable(String hostNameOrIp) {
boolean isRechable = InetAddress.getByName(hostNameOrIp).isReachable(30);
return isRechable;
}
Parameter can be "http://192.168.0.77:8080/ws/api/customer/5" or "http://example.com:8080/ws/api/customer/5"
But everytime it's returning false although I can browse the URL.
What do you mean "reachable"?
If "open the page" means "reachable", you can just try to use an HTTP connection to connect to it. If you got exceptions: your net work might down; you cannot connect to the host;
Then you shall check the http return code, to see if there are 404 or other errors.
I am trying to connect to a MySQL database in an Android application but am unable to do so.
The URL I'm using for the connection to a MySQL database is: jdbc:mysql://xx.xx.x.xxx/dbname, where xx.xx.x.xxx represents the IP address of my server. The database dbname is managed by phpMyAdmin on this server.
But I always receive the same error : com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
I have the internet PERMISSION in my manifest.
The code I'm using:
private void callSQL() {
java.sql.Connection laConnection;
java.sql.Statement transmission;
ResultSet leResultat;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
laConnection= DriverManager.getConnection("jdbc:mysql://xx.xx.x.xxx/dbname", "user", "passwrd");
transmission = laConnection.createStatement();
leResultat= transmission.executeQuery("select * from Communes");
while (leResultat.next()) {
System.out.println("Commune : "+ leResultat.getString("Nom"));
}
}catch(Exception e){
System.out.println(e);
}
}
I have been able to use another solution with a HTTP request on a PHP page that connects to the database. This solution works well, but which one is the best way to use?
I found the solution. The problem wasn't the code. The problem is the connection. I hadn't the permission to connect the database. With the permission allowed to a distant user on the database, it was OK.
I had this code that worked well last Thursday (04/19/2012):
Connection conn = null;
Statement inst;
try {
System.out.println("Connected to the database");
Class.forName ("net.sourceforge.jtds.jdbc.Driver");
//217.126.108.151
conn = java.sql.DriverManager.getConnection("jdbc:jtds:sqlserver://localhost:1433/prueba;sa;sa1234");
inst = conn.createStatement();
System.out.println("Connected to the database");
inst.executeUpdate("INSERT INTO TIENDAS (NOMBRE) VALUES ('ZZZZZZZ') ");
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
But after the last update of the Android SDK stopped working.
Now I get this error:
java.lang.RuntimeException: No message resource found for message property prop.servertype
Does anyone know why?
Thanks in advance.
Clean your project and rebuild at again. Still if your getting the same problem
try the following way..
-> Instead of localhost in url use this ip : 10.0.2.2
I'm not sure, but since Android >=3.0, network connection have to run in seperate Thread or Task. But if this is the problem, you should get an other exception.
But give it a try :)
And don't forget to check the permissions in your Manifest.
I'm trying to invoke a webservice and parse the returned XML, so my invoke URL is :
http://192.168.1.12/cr.ws/CarCategories.asmx/CarCategoryComparatorRQ?IdUser=1076&IdCurrency=1&IdLanguage=1&IdCarCategory=0&IdPickupRentCarAgencies=3&IdReturnRentCarAgencies=3&PickupDate=6/4/2011&ReturnDate=9/4/2011&Supplier=Prima%20Rent%20a%20Car&b=prima
in my browser I get the desired response :
<CarCategoryComparatorRS>
<CarCategory>
<IdCar>22</IdCar>
<IdCarCategory>22</IdCarCategory>
<Supplier>AvantGarde Rent a Car</Supplier>
<PickupRentCarAgencies>Tunis; Aéroport de Tunis-Carthage</PickupRentCarAgencies>
<IdPickupRentCarAgencies>5</IdPickupRentCarAgencies>
<ReturnRentCarAgencies>Tunis; Aéroport de Tunis-Carthage</ReturnRentCarAgencies>
<IdReturnRentCarAgencies>5</IdReturnRentCarAgencies>
<IdUser>705</IdUser>
<Title>Catégorie A [ex:Kia Rio;Renault Symbol]</Title>
<IdCurrency>1</IdCurrency>
<ImageUrl>Car-22-20090521-125545.jpg</ImageUrl>
<MemberReductionValue>0</MemberReductionValue>
<Rate>150</Rate>
<DelayRate>0</DelayRate>
<ReductionValue>135</ReductionValue>
<DayRate>45</DayRate>
<Reduction>10%</Reduction>
<AccompteValue>67.500</AccompteValue>
<Accompte>50</Accompte>
<TotalRate>0000135000</TotalRate>
</CarCategory>
</CarCategoryComparatorRS>
But this function trows java.io.FileNotFoundException
protected InputStream getInputStream() {
try {
return feedUrl.openConnection().getInputStream();
} catch (IOException e) {
throw new RuntimeException(e);
}
Please help me, I wasted couple hours searching but nothing :(
Thanks.
The error is caused by the spaces in this param :"Supplier=Prima Rent a Car", so i replaced this param by UrlEncoder.encode("Supplier=Prima Rent a Car") and it worked.
If you type that URL in your device's browser address bar do you get anything?
It looks like you are trying to request an address on a local machine or LAN. Are you requesting this on the android device? My guess is you need to run your server on the machine on a different interface or open up connections to the required port.
I think your url is being interpreted as a file url.
What about logging your url before you call openConnection as a sanity check:
Log.e("MyApp", feedUrl);
On second thought, could you post the logcat output? I'm skeptical as to how your code could ever throw a FileNotFoundException.