How can I connect to web page, get status code to check if page available or not
Clash Royale CLAN TAG#URR8PPP
How can I connect to web page, get status code to check if page available or not
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();
Possible duplicate of Unfortunately MyApp has stopped. How can I solve this?
– Vladyslav Matviienko
Aug 10 at 5:47
1 Answer
1
First of all HTTP requests are no longer support on the main thread so you must use a parallel Thread, or simple use an AsyncTask. Check below code for AsyncTask.
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
output3 = findViewById(R.id.output3);
output3.setMovementMethod(ScrollingMovementMethod.getInstance());
AsyncTask.execute(new Runnable()
@Override
public void run()
connection();
);
Second thing is make sure Internet permission is added in your manifest.
Noted & thank you very much sir, it finally works! :D
– Renaissence_x
Aug 10 at 7:16
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
erm, just a suggestion. You may consider using switch (case default) to check the responseCode then disconnect from the connection. Related to this codereview.stackexchange.com/questions/45819/…
– 薛源少
Aug 10 at 5:32