I new to android programming and I had some problem that needs help, I'm stuck at this part for quite some time.
Problem: the code is successfully compiled, but making the connection to check make it crash. Can you help me by telling what is causing the issue and suggest me the solution or help improve my code. Thank you.
Process: the app check whether the app it wants to check is exist in google play store or not by checking its package name by sending simple HTTP connection. If the connection success to the app page in google play store, it will output a string that the page exists and it's a legit app.
Here is a snippet of my code
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
output3 = findViewById(R.id.output3);
output3.setMovementMethod(ScrollingMovementMethod.getInstance());
connection();
}
public void connection(){
try {
String packagename = "com.facebook.katana";
URL url = new URL("http://play.google.com/store/apps/details?id="+packagename+"&hl=en");
URLConnection urlConn = url.openConnection();
HttpURLConnection con = (HttpURLConnection) urlConn;
con.setUseCaches(false);
con.setAllowUserInteraction(false);
con.setRequestMethod("GET");
con.connect();
int status = con.getResponseCode();
if (status == HttpURLConnection.HTTP_NOT_FOUND){
output3.append("not from google play store");
}
if (status == HttpURLConnection.HTTP_OK) {
output3.append("google App Store");
}
if (status != HttpsURLConnection.HTTP_NOT_FOUND && status != HttpsURLConnection.HTTP_OK)
output3.append("Other Response");
con.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}