@Mr_Kurator

Как к webview html сайту подключить java скрипты?

На webview html сайте перед редиректом нужно поймать рефер и передать на ссылку которую редиректим. Ниже два java скрипта которые это делают в приложении. Как их прикрутить к html сайту. Спасибо

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();
    }
}
  • Вопрос задан
  • 72 просмотра
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы