Android application connect to local area Wamp Server - android

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

Related

Uploading a file through HTTPS port using httpsurlconnection class

I have a server that only opens a port using port number 443. I need to upload a file to that server using Android client. My question is:
is it possible to connect and upload to such server using httpsurlconnection or do i need to use other class?
if it is possible, can you please show me an example of it?
thanks.
You should be able to do that simply by creating a URL with 'https' instead of 'http'.
Example from documentation:
URL url = new URL("https://www.android.com/");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try {
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
readStream(in);
} finally {
urlConnection.disconnect();
}
Refer to the documentation for details:
https://developer.android.com/training/articles/security-ssl.html#HttpsExample
https://developer.android.com/reference/java/net/HttpURLConnection.html

Getting access to localhost by using webservice in my Android device

I'm trying to test an Android aplication that uses webservice to access localhost.
My device ip is 192.168.0.21 and my localhost ip is 192.168.0.22.
I tried this:
HttpsURLConnection conn;
Url url = new URL("http://192.168.0.22:80/admapp/adm_service.php?");
conn = (HttpsURLConnection) url.openConnection();
It does not work. I've tried to disable the firewall and nothing.
I'm using EasyPHP, but I think that this don't matter.
Could anyone help me?
It should work if you map your IP to localhost in hosts file. The file will be at C:\Windows\System32\drivers\etc\hosts in windows, /etc/hosts in Linux.

Openssl handshake failed when using getInputStream() on Facebook profile picture in android sdk

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.

Android how to make GET or POST Http Request using url with an IP address instead of HostName

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.

Internet connection not working on some networks

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.

Categories

Resources