Network Connection - android

I am trying to call a webservice from an android app (Testing from an Emulator)
Now, i keep on getting "request time failed : java.net.SocketConnection : Address family not supported by protocol"
So, went to 'Browser' app of Android-Emulator and wasn't able to open any website
Also, then wrote a java code (J2SE) for network connection and got a "java.net.ConnectException: Connection timed out: connect"
Then looked here and there and guys had a say that some setting are to be done in eclipse and android-emulator to get the network connection going
Like in one of messages here --> "http://192.9.162.102/thread.jspa?messageID=10631928"
But how and where is what i am looking for ?
Any suggestions ?
Thanks
Yogurt

put this line<uses-permission android:name="android.permission.INTERNET"></uses-permission> in Manifest.xml file. I think it will work for u.

Related

How to solve SocketException: Failed host lookup: 'www.xyz.com' (OS Error: No address associated with hostname, errno = 7)

Whenever I try to do an http call after about 20 seconds I get in the console the following error:
E/flutter ( 8274): [ERROR:flutter/shell/common/shell.cc(184)] Dart Error: Unhandled exception:
E/flutter ( 8274): SocketException: Failed host lookup: 'flutter-project-xxxxx.firebaseio.com' (OS Error: No address associated with hostname, errno = 7)
This error happens for every method and every route I call through the app http package.
I'm developing a flutter app on Windows, using an AVD virtual device from Android Studio.
Versions:
http: ^0.12.0+1
flutter: 1.0
Cases:
From home or though tethering from my phone:
connectivity works fine on every part of the virtual device
From my work network (behind the firewall):
Only with Android Web View, I can browse the internet without any problem (i can even call the same url, I use in the code and it works).
I get a connection error when using any other application (Chrome, Google Play, ecc...) in the virtual device, and specifically the "SocketException", when testing my app.
What is the difference between the calls coming from that app?
Is there a way to route my app calls the same way as the ones in the webview?
Thanks!
Adding internet permission is not only a solution.
You also have to make sure that you are online whether it is mobile or emulator
Make sure you are online whether it is mobile or emulator
Make sure you have given internet permission in your app's android/app/src/main/AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET"/>
Add <uses-permission android:name="android.permission.INTERNET" /> to your AndroidManifest.xml file.
I added <uses-permission android:name="android.permission.INTERNET" /> to
my manifest. I then had to restart the emulator for internet to work.
Make sure you have added the in android/app/src/main/AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
If you are using an emulator make sure that the mobile data is active
You should try following:
Try run application in debug mode if its working fine and not working in release mode then it might be related to permission issue so add permissions(INTERNET, ACCESS_NETWORK_STATE) in manifest file.
your_project_root_directory/android/app/src/debug/AndroidManifest.xml
your_project_root_directory/android/app/src/main/AndroidManifest.xml
your_project_root_directory/android/app/src/profile/AndroidManifest.xml
If not included then add permissions:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
If its not working in debug mode, then check either your url is correct by hitting in postman. If its working in postman then you should execute your webservice in this way:
Add dependency in pubspec.yaml:
dependencies:
flutter:
sdk: flutter
http: 0.13.3
After adding http:0.13.3 then click on Pub get button If its executed with code 0 then it means its successfully added to your project.
in your dart file import http.dart:
import 'package:http/http.dart';
Now write this code to execute webservice:
var url = Uri.parse('some-url-login?username=abc&&password=xyz');
var response = await get(url, headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': 'true',
'Access-Control-Allow-Headers': 'Content-Type',
'Access-Control-Allow-Methods':
'GET,PUT,POST,DELETE'
});
if (response.statusCode == 200) {
// success
} else if(response.statusCode == 404){
// not found
} else if(response.statusCode == 500){
// server not responding.
} else {
// some other error or might be CORS policy error. you can add your url in CORS policy.
}
I hope it will help you. Chill!
Go for AVD manager, then choose your emulator, on Actions menu click down option sign, then choose wipe data, then restart your emulator. It works for me.
Even I had the same issue. I restarted my emulator and it worked!!
If you are getting this error while using the flutter_socket_io plugin, don't forget to initialize the socket i.e socketIO.init(); socketIO.connect(); before attempting to subscribe to it. I received the same error as that indicated above when I omitted it.
By enabling WIFI it works for me, and make sure you also added the internet permission in the android manifest it added automatically also but you have to make sure. if still not then invalidate caches and restart hope it works
In my case it was because my phone was not connected to internet
Just enable internet connexion
Make sure your Mobile device or Emulator is connected to the internet
In My case I opened the emulator before I connected to the WIFI to system.When network(WIFI) has connected to my system but unable to connect to emulator .
Due to no internet access to the Emulator,this issues occurs.I resolved this issues by this way.
Solutions:
Connected network access to the system then I Closed the emulator and re-open it. Install applications ,run application and open it .And Finally works every API works fine.
When i enabled wifi on my device, it worked fine.
Even after adding
<uses-permission android:name="android.permission.INTERNET"/>
in your android/app/src/main/AndroidManifest.xml, if you're still finding trouble then
There maybe an error with the website's server IP address like:
Server IP address could not be found
It's better to wait for some time and make sure to restart wifi, it worked for me.
Make sure your Emulator or your device connected to the internet.
if your Emulator could not connect to the internet use a VPN.
The problem for me was that I was using a VPN. The emulator didn't have internet access when connected to the VPN on my computer. It might help someone.
I'm using arch Linux.
Linux arch 5.14.7-arch1-1
And Flutter version
Flutter 2.2.1
Dart version
Dart 2.13.1
Code editor
1.58.0
In my case turning on wifi solved the problem.
Check if the wifi is connected or not. Mine worked after restarting the wifi.
Make sure the internet is on (both on the phone and the PC)
also, make sure the server is working properly
check your URL properly
Sometimes, other wifi can have interference with your connected wifi, which can cause this error as well. Try to set your connected wifi's channel to auto or away from other wifi's channels.
I had the same problem while running the app on an emulator.
My laptop was connected to the internet through my phone's mobile hotspot.
But apparently the emulator was unable to access the internet
Later, I switched to my Wi-Fi Router connection and the application proceeded smoothly as expected.
I'm not sure, but is there any special access to be granted?
Tip: Try googling something on the emulator to check internet connectivity
For me it was just bad url with function "Uri.https(...)" in Flutter.
Check if the address is correct with your browser
The DNS settings on my mac were invalid...just use 8.8.8.8 without any internal DNS routing addresses
The problem is basically coming when device is trying to access any internet resources,to fix add the
to AndroidManifest.xml file #android/app/src location.
Check if you have enabled offline work in the gradle settings.
File-> Settings -> Build -> Build Tools -> Offline work
If its enabled or checked then uncheck it.
Also, might good to look at the 'Proxy settings' inside the 'Extended controls' of the emulator.
The way it worked for me was to change to use the 'No Proxy' setting.
Double-check / make sure you have
<uses-permission android:name="android.permission.INTERNET" />
in your app's android/app/src/main/AndroidManifest.xml
if you're using vscode you would get gai_error=0 that's if the http is connected successfully
Here is the right place to place the internet permission because it was quite challenging to know the location
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.appName">
<uses-permission android:name="android.permission.INTERNET" />
run the flutter run command on cmd or visual studio code terminal
I deliberately disconnected my emulator wifi to see what would happen (as there could be cases where no internet is available). The error led me here, but, all I see are workarounds and not error handling. Really, the app should be able to handle the error gracefully and not crash out because of a lack of connection.
So far, all I have done is to catch the error and if the return code is -1 (or anything other than a 200), I know I have a problem and can make the app respond appropriately.
var request = new http.MultipartRequest("POST", uri);
// multipart that takes file
var multipartFile = new http.MultipartFile('file', stream, length,
filename: fileToUpload.path);
// add file to multipart
request.files.add(multipartFile);
// send
try {
var response = await request.send();
print(response.statusCode);
// listen for response
response.stream.transform(utf8.decoder).listen((value) {
print(value);
});
return response.statusCode;
}
catch (e)
{
return -1;
}

Connecting Tizen watch with Android phone returns FINDPEER_SERVICE_NOT_FOUND

Hello everyone I've been trying to connect a GearS2 device with an Android phone using the accessory SDK, but every time the service is created and I start the connection the method "onFindPeerAgentResponse" is returning the peer as null and the result integer 1794 which is FINDPEER_SERVICE_NOT_FOUND.
I don't know if I'm doing something wrong in the config files, the manifest or the service profile.
Did you manage this? I would say to take a look at the Samsung example provided for a companion app # http://developer.samsung.com/gear check the "COMPANION TYPE : HELLO ACCESSORY" It helped me a lot.
Share some code if this example doesn't help find the problem.
Cheers,
Ricardo

Timeout when making HTTPS requests in Android Emulator

I'm trying to make HTTPS request using the HttpURLConnection class. When the URL starts with "http://" the connection responds as expected after calling .connect(). The problem is when the URL starts with "https://", the connection always times out (after 5000ms, the timeout I set). The exception message is:
failed to connect to [url] (port [XXX]) after 5000ms
I'm using the Parse SDK for Android and every query I make times out as well. The exception message is:
com.parse.ParseRequest$ParseRequestException: i/o failure
and
org.apache.http.conn.ConnectTimeoutException: Connect to /54.84.111.80:443 timed out".
So I think the Parse SDK always uses HTTPs behind the hood.
Some things I think I need to point out:
I'm using the emulator with the Android 5.1.1 x86_64.
The internet I'm using has a proxy and the emulator is being started with the -http-proxy command line option.
I can't deactivate this proxy because I'm using the internet of the company I work for.
I have an application written in .NET that uses the Parse SDK as well, and it runs fine using the same internet connection.
<uses-permission android:name="android.permission.INTERNET"/>
try using this tag instead of
<permission android:name="android.permission.INTERNET"></permission>
if the problem still persists then it could be proxy problem

Google cloud messaging GCM through proxy

I am having trouble sending a message to a device through the company proxy.
I have done the GCM tutorial on the google site and have successfully registered a device on the google servers with the android emulator,
and also on the server at my end.
For this I had to go through the company proxy, setting it in the access point of the emulator.
Now the problem I have is sending a message from my server to the device through the same proxy.
I am using the servlet code from the demo which uses the
com.google.android.gcm.server.Sender
helper class to send the message.
I am running the servlets on tomcat 7.
I have tried setting the proxy up in the catalina.properties file like so.
http.proxyHost=proxy.company.com
http.proxyPort=8080
And I have tried setting properties inside the servlet itself like below.
System.setProperty("http.proxyHost", "proxy.company.com");
System.setProperty("http.proxyPort", "8080");
But still I get the timeout.
I know its the company proxy because I got the messaging working at home where I have no proxy.
I have seen a proxy object created in the java code and then a connection created with it, but I dont think that is usable here since I am using Sender helper class to send the message.
This is the line that fails in a timeout.
Result result = sender.send(message, registrationId, 5);
Any help would be appreciated.
Regards
Bill
Alright, I finally have it working. In my comment I mentioned that I had succeeded in sending out the message, but the emulator was not recieving it. I had forgotten to follow my own earlier advice and run the emulator from the command line with the proxy parameters set like so:
emulator.exe -avd avd22google -http-proxy proxy.company.com:8080 -debug-proxy
So to summarise, my initial problem was that I had registered the emulator on the with GCM and with my local server, but the when I clicked send message I was getting a timeout.
I initially thought it was the firewall so I did some research and set up the proxy in tomcats catalina.properties file.
This made no difference.
I used the "Charles" web proxy debugger software to see where the message was attempting to be sent to and it came up with https://android.googleapis.com:443
So I initially I added the following to my catalina.properties file:
https.proxyHost=proxy.company.com
https.proxyPort=443
It still did not work. A colleague of mine told me that our company proxy handles all types of requests through port 8080, so I changed the the poort line to:
https.proxyPort=8080
This allowed the message to be sent out.
But then the message was not getting through to the emulator and I was receiving the following error in LogCat.
[GTalkConnection.12] doConnect: caught XMPPError connecting to mtalk.google.com:5228.: -- caused by: java.net.SocketException: The operation timed out
Then I remembered that you need to start the emulator with the command line to get it to use the proxy. Once I did this a flood of messages appeared on my emulator!
So I finally have it working end to end. It's taken me about a week to get GCM fully working within my company firewall, so hopefully this post can help some other poor sod doing this in the future.
cheers
Bill

Can't get SipDemo to register

I am developing an application that will make SIP calls using the SIP APIs. I want to use the SipDemo code as my starting point, but when I run SipDemo on my devices (Nexus or Galaxy Tab) I always get "Registration failed. Please check settings." I am using the same username/password/server information that works successfully in Sipdroid.
I inserted log messages in setRegistrationListener to follow what's happening, and I will see onRegistrationDone fire, but then it seems to register again, because it will then see Registration failed with the error message "registration timed out" followed by registration failed with the error message "cannot initiate a new transaction to execute".
I've been beating my head about this for a few days, and I've got a deadline rapidly approaching. Any help on this issue would be greatly appreciated, or if you could point me to some other examples/tutorials of how to use the SIP APIs.
I fixed it! The demo wants to use UDP by default, but I was able to make it connect by hardcoding the protocol to TCP. I have a netgear modem from charter cable, and I have seen several reports of it blocking access. I assume the two are related, but for now I'm just happy to have it work so I can continue my development.
Thanks for the help!
Check the logcat, it should have more information about what failed. Also if you have control over the server, check the server logs as well.
Seems like you are not alone http://code.google.com/p/sipdroid/issues/detail?id=60.
I would suggest that you try what people tried there: i.e.
Try another router.
Change port in code. Read the link for more details.

Categories

Resources