-1

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();
        }
    }
  • erm, just a suggestion. You may consider using switch (case default) to check the responseCode then disconnect from the connection. Related to this https://codereview.stackexchange.com/questions/45819/httpurlconnection-response-code-handling – 薛源少 Aug 10 '18 at 05:32
  • Possible duplicate of [Unfortunately MyApp has stopped. How can I solve this?](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – Vladyslav Matviienko Aug 10 '18 at 05:47

1 Answers1

0

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.