M
M
Mr_Kurator2020-01-18 02:15:07
Java
Mr_Kurator, 2020-01-18 02:15:07

How to connect java scripts to webview html site?

On a webview html site, before redirecting, you need to catch the referrer and pass it to the link that we are redirecting. Below are two java scripts that do this in the application. How to attach them to the html site. Thanks

InstallReceiver.java

public class InstallReceiver extends BroadcastReceiver {
    private InstallReferrerClient m_referrerClient;
    private String m_url = "https://a.d8g.xyz/app/ref";

    public InstallReceiver() {
        this.m_referrerClient = null;
    }

    public void onReceive(final Context context, final Intent intent) {
        try {
            String uuid = UUID.randomUUID().toString();

            Map<String, String> postData = new HashMap<>();
            postData.put("type", "intent");
            postData.put("uuid", uuid);
            Bundle bundle = intent.getExtras();
            Set<String> keys = bundle.keySet();
            for (String key : keys) {
                Object o = bundle.get(key);
                postData.put(key, o.toString());
            }
            new HttpPostAsyncTask(postData).execute(m_url);

            (this.m_referrerClient = InstallReferrerClient.newBuilder(context).build()).startConnection(new InstallReferrerStateListener() {
                @Override
                public void onInstallReferrerServiceDisconnected() {
                }

                @Override
                public void onInstallReferrerSetupFinished(final int n) {
                    if (n == 0) {
                        try {
                            final ReferrerDetails installReferrer = InstallReceiver.this.m_referrerClient.getInstallReferrer();
                            installReferrer.getInstallReferrer();
                            Map<String, String> postData2 = new HashMap<>();
                            postData2.put("type", "client");
                            postData.put("uuid", uuid);
                            postData2.put("referer", installReferrer.getInstallReferrer());
                            new HttpPostAsyncTask(postData2).execute(m_url);
                        } catch (RemoteException ignored) {
                        }
                    }
                }
            });
        } catch (Exception ignored) {
        }
    }
}

HttpPostAsyncTask.java

public class HttpPostAsyncTask extends AsyncTask<String, Void, Void> {
    JSONObject postData;
    public HttpPostAsyncTask(Map<String, String> postData) {
        if (postData != null) {
            this.postData = new JSONObject(postData);
        }
    }

    @Override
    protected Void doInBackground(String... params) {

        try {
            // This is getting the url from the string we passed in
            URL url = new URL(params[0]);

            // Create the urlConnection
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

            urlConnection.setDoInput(true);
            urlConnection.setDoOutput(true);

            urlConnection.setRequestProperty("Content-Type", "application/json");

            urlConnection.setRequestMethod("POST");

            // Send the post body
            if (this.postData != null) {
                OutputStreamWriter writer = new OutputStreamWriter(urlConnection.getOutputStream());
                writer.write(postData.toString());
                writer.flush();
            }

            int statusCode = urlConnection.getResponseCode();

            if (statusCode ==  200) {

                InputStream inputStream = new BufferedInputStream(urlConnection.getInputStream());

                String response = convertInputStreamToString(inputStream);

                // From here you can convert the string to JSON with whatever JSON parser you like to use
                // After converting the string to JSON, I call my custom callback. You can follow this process too, or you can implement the onPostExecute(Result) method
            } else {
                // Status code is not 200
                // Do something to handle the error
            }

        } catch (Exception e) {
//            e.printStackTrace();
        }
        return null;
    }

    private String convertInputStreamToString(InputStream inputStream) {
        BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
        StringBuilder sb = new StringBuilder();
        String line;
        try {
            while((line = bufferedReader.readLine()) != null) {
                sb.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sb.toString();
    }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question