雖然@RivieeaKid的html解析解決方案有效,但我發現這可能是更持久和正確的解決方案。請確保使用'https'前綴(不是簡單的'http')來避免重定向。
/**
* Checks if an app with the specified package name is available on Google Play.
* Must be invoked from a separate thread in Android.
*
* @param packageName the name of package, e.g. "com.domain.random_app"
* @return {@code true} if available, {@code false} otherwise
* @throws IOException if a network exception occurs
*/
private boolean availableOnGooglePlay(final String packageName)
throws IOException
{
final URL url = new URL("https://play.google.com/store/apps/details?id=" + packageName);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.connect();
final int responseCode = httpURLConnection.getResponseCode();
Log.d(TAG, "responseCode for " + packageName + ": " + responseCode);
if(responseCode == HttpURLConnection.HTTP_OK) // code 200
{
return true;
}
else // this will be HttpURLConnection.HTTP_NOT_FOUND or code 404 if the package is not found
{
return false;
}
}
要小心,並仔細閱讀與Google Market的許可協議細則,這可能違反了它。 – JPM