1

I am developing an android app, and plan to make get requests to sites such as youtube and spotify. When I run this code through Eclipse, I get the response that I want. If I were to convert this code into an android app, will I still be able to generate the same results, or would I need the API's of youtube and spotify to do so?

This is the code I used to take a spotify song link and simply extract the song name from it. I do not want to use the Spotify API for such a basic function. If I were to put the code into an app, will I still be able to make the get request and get the information necessary?

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.*;

public class SpotAlgo {
String vidLink;
int linkLoc;
String testString = "<title>";
String result;


public String gettheResult(String v) throws Exception{
    String sname = "";
    vidLink = "https://open.spotify.com/track/6b8Be6ljOzmkOmFslEb23P";

    URL obj = new URL(vidLink);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();
    con.setRequestMethod("GET");
    con.connect();
    BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
    String inputLine = "";
    StringBuffer response = new StringBuffer();

    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();

    result = response.toString();


    linkLoc = result.indexOf(testString) + testString.length();
    for (int i = linkLoc; i < result.indexOf("on Spotify"); i++) {
        sname += result.charAt(i) + "";
    }
    return sname;
}

}

  • Yes it can be done as all the classes which are used in your code are also available in Android API. – Monish Kamble Nov 24 '16 at 05:54
  • It is totally valid to use `HttpURLConnection` in Android, and this should work in Android if it worked as a standalone application. However, you should double check the terms of service with Spotify. They may require you to use a special API for Android. – Tim Biegeleisen Nov 24 '16 at 05:55
  • Request works fine for me [using Volley](https://developer.android.com/training/volley/index.html). Your issue is not evident without a [mcve], as in, we would like to see the AsyncTask, and all other related code around calling this class. – OneCricketeer Nov 25 '16 at 05:08
  • Do be aware: You are using `HttpURLConnection` when connecting to an `https://` address. – OneCricketeer Nov 25 '16 at 05:10
  • @cricket_007 what is wrong with https:// included in the URL? – Programmer87 Nov 25 '16 at 05:34
  • Like if the website needed a signed, secure request over SSL, then the HttpURLConnection class would throw some error about an invalid certificate, or something like that. – OneCricketeer Nov 25 '16 at 05:46

1 Answers1

0

The Java libraries that you are using are all supported in Android. It should work in technical terms, as it has worked for me before. However, you should check with the services TOS to make sure it is within their rules.

darkterbears
  • 169
  • 12
  • Have you made an application that has interacted with Spotify in a similar way? Would I have to register this project on their dev page or something like that? – Programmer87 Nov 24 '16 at 06:05
  • I have not made an app that connects w/ Spotify, but I used coded similar to yours that connected with my own server. You might want to check with their Dev TOS: https://developer.spotify.com/developer-terms-of-use/ – darkterbears Nov 24 '16 at 06:44