I have an app that reads in an XML file, parse it and display certain elements from the xml file to the user. The problem I am having is that the network connection to the server that contains the XML file works on some networks but not on others. For example it works on my home network and the mobile 3g that comes with the phone but when I test it on the University network and the office network it cannot get a connection.
I don't understand why this is and don't know how I would fix it. I am using an HttpURLConnection to connect to the server.
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("GET");
conn.setDoInput(true);
urlString is the web address. I have added the permission on manifest.
<uses-permission android:name="android.permission.INTERNET" />
most probably,open Internet is not available on your University /Office network or might be the url you are trying to access is blocked on these networks.
Try to browse the same url through Browser App in your phone. This will give you a better picture of the problem.
Related
I need to connect my app to the wamp server located in my machine within my LAN, in order to test an api I am currently developing. While using the phone internet browser, I can access the ip address for api viewing, but using something similar to the following code does not work.
URL url = new URL("192.168.1.1/api/v1/home");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
Please give some suggestions in order to be able to test this.
Try adding http before the link
URL url = new URL("http://192.168.1.1/api/v1/home");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
Are you using the emulator? If so change your IP to this: http://10.0.2.2
I am trying to create user profile page for my app.
I succeed with connecting to Facebook, so i have the user basic profile using userProfile = Profile.getCurrentProfile();
Now i am trying to create a drawable of the user profile picture to put in a image view. For this I created a inputstream to fetch the image(running on asyncTask)
Bitmap x;
url = new URL("https://graph.facebook.com/939344862743414/picture?height=200&width=200");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("User-agent","Mozilla/4.0");
connection.setConnectTimeout(100000);
connection.connect();
BufferedInputStream buf = new BufferedInputStream(connection.getInputStream());
x = BitmapFactory.decodeStream(buf);
However no matter what I do,the app failed and crash when it reach the connection.getInputStream(). The error is javax.net.ssl.SSLProtocolException: SSL handshake aborted
I couldn't find any help on the web .It doesn't seem as an authorization problem because I can connect to Facebook and fetch data.
What I am doing wrong?
The issue is created by the url. As the URL contains https which means the web server certificate is issued by a well known CA, so that you can make a secure request. As you are using HttpURLConnection it doesn't support secured URL.
So you can enable the access to secured url (https://..) by using URLConnection in the place of HttpURLConnection. So modify the following line:
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
with this one:
URLConnection connection = (URLConnection) url.openConnection();
Detail information can be found here.
I am wondering if we can access the network through HttpURLConnection from Android Wear?
I tried using HttpURLConnection inside Wear code, I am getting EOFException. The same code works from regular Android phone. It only has problem when it is on Android Wear.
If HttpURLConnection is not supported on Wear, should we use Apache Http client or something else?
Or perhaps the way I am launching the app for development is incorrect?
URL url = new URL(myurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("GET");
conn.setDoInput(true);
// Starts the query
conn.connect();
int response = conn.getResponseCode();
I did add the permission into android manifest. I also run the above code from an AsyncTask.
EOFException occurs at conn.getResponseCode().
java.io.EOFException
at com.android.okhttp.internal.Util.readAsciiLine(Util.java:342)
at com.android.okhttp.internal.http.RawHeaders.fromBytes(RawHeaders.java:311)
at com.android.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:135)
at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:644)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:353)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:297)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:509)
Thank you very much for your help.
Unfortunately, no.
Android Wear applications cannot directly access the Internet. They must communicate with their corresponding handheld app (either via MessageApi or DataApi) and request that it executes whatever HTTP requests you need.
EDIT: Android Wear 2.0, now in beta, supports network requests, so HttpURLConnection should work there.
I know how to check if my apps is connecting to wifi, and get XML string and parse it to my apps, but i want to know how to check if a site with xml ready to parse is already activate from the server so that i can get the XML and parse, because if that site is not activated from the server ill get the error "No Route to Host".
How to Check if that link is ready and activated from the server?
When resource was not found, your web server should set response code to 404.
If you're using HttpUrlConnection
URL url = new URL("www.example.com");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
if(conn.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND ){
handleError(); // your custom error handler
}
Hi all I am trying to send data to my development database from android application using Post but am getting exception
09-18 15:23:36.801: W/System.err(6755): java.io.FileNotFoundException: http://10.255.162.84:8080/web/events
09-18 15:23:36.801: W/System.err(6755): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:1162)
If I copy paste the url the web browser in my computer. the webpage I am trying to get works.
If I replace the url with https:www.google.com in my application, it works.
What does this mean? I can't connect make http connection without a host name?
code
URL url = new URL("http://10.255.162.84:8080/web/events");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setReadTimeout(20000 /*millis*/);
conn.setConnectTimeout(15000 /*millis*/);
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.setDoOutput(true);
conn.connect();
System.out.println(conn.getResponseCode());
InputStream is = conn.getInputStream();
Reader reader = new InputStreamReader(is, "UTF-8");
char[] buffer = new char[500];
reader.read(buffer);
System.out.println(new String(buffer));
Additional information
The server to receive get and post requests is running on my PC port 8080 via a broadband connection.
I am running the application on my low end phone running android 2.2 (Froyo)
I hope that's all the relevant information but am glad to provide more. I know you get this all the time but I swear to you I am really new to android
I'm guessing your android device is not on the same network as your computer. You can test this easily by pasting the url into an android browser and checking to see if it works. Your server is most likely behind your network's firewall. You'll need to configure port forwarding to allow external clients to connect.