Android App HTTP connection to server (Server upgrade to HTTPS) - android

I has an android app that AndroidManifest set to usesCleartextTraffic="true" because it is using http to connection to server.
But, now i plan to update the server to use HTTPS, and the server will redirect all http to https.
So, i want to know, if server redirect all traffic to HTTPS, will the android app that access the server using HTTP still working?
Example i am using the java code below to access server content. So will app still able to read the content at the app.php, if server update to use HTTPS? (server set to redirect all http to https)
String urls = "http://www.example.com/app.php";
URL url = new URL(urls);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Cache-Control", "no-cache");
content = readStream(conn.getInputStream());
Thanks in advance

Related

Spotify Android - Make Authorized Web Requests

I'm working on an Android app that implements the Spotify API to allow the users to listen to music. I've configured the Player that Spotify has created for android devices, but it's incredibly limited in terms of its functionality, so I've had to go through Spotify's Web API to do more advanced features.
I've hit a bug when trying to get a list of the user's own playlists. I'm making a request using:
URL url = new URL("https://api.spotify.com/v1/me/playlists");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
But instead of this command going through like it does for the other web API requests I've made, it throws the error:
java.io.FileNotFoundException: https://api.spotify.com/v1/me/playlists
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:242)
at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)
at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:25)
at com.tmacstudios.spotifyvoice.WebWrapper$override.searchUserPlaylist(WebWrapper.java:257)
at com.tmacstudios.spotifyvoice.WebWrapper$override.access$dispatch(WebWrapper.java)
at com.tmacstudios.spotifyvoice.WebWrapper.searchUserPlaylist(WebWrapper.java:0)
at com.tmacstudios.spotifyvoice.MainActivity$6.run(MainActivity.java:382)
at java.lang.Thread.run(Thread.java:818)
I'm not entirely sure why I'm getting this error, but I think it may have to do with not having the necessary authorization to make this request. Can anyone please help me solve this problem?
After studying the documentation some more, I was able to figure out a way to solve my problem. I was in fact getting the error since I wasn't using the proper authorization.
You can get the authorization token in OnActivityResult using this code:
AuthenticationResponse response = AuthenticationClient.getResponse(resultCode, intent);
if (response.getType() == AuthenticationResponse.Type.TOKEN) {
authToken = response.getAccessToken();
Log.e("MainActivity","Auth Token: "+authToken.toString());
...
Then when making the URL request, just pass in the authorization token as a header.
URL url = new URL("https://api.spotify.com/v1/me/playlists");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestProperty("Authorization", "Bearer " + authToken);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
...
Also note that you must have the permissions you are using enabled as scopes when you are forming the authorization request initially.

Using HttpURLConnection or URLConnection with https domains

I'm using HttpURLConnection and URLConnection to connect to some domains on my apps to download configurations, images, etc...
Now, I must switch all the domains to https (because IOS needs it). Should my code on Android using HttpURLConnection and URLConnection work with the new https domains or it is mandatory to migrate the source code to be compatible with https domains?
When you open a https URL, a HttpsURLConnection is returned which extends HttpURLConnection. Your code should work fine, but consider handling SSL Exceptions.

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.

How to check if that xml parsing site is Up and running or is on from server?

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
}

Can't completely avoid redirection through HttpURLConnection

Eventhough I have set to not follow redirects:
HttpURLConnection urlConnection = url.openConnection();
urlConnection.setInstanceFollowRedirects(false);
I still can't get my POST request to work. I get response code: 302 (it means the website offers me redirection) and I think this is the reason because everything works perfectly with other websites that doesn't redirect. How to completely avoid redirection so to make my POST request to work?

Categories

Resources