java - 从一个类在 webview 中打开 url?

标签 java android json android-intent webview

我有一个类 InitilizeSDK,我在其中使用用户输入的变量 developer_public_key 和字符串 offerwall_public_key 初始化 Url。我在 onPostExecute 中使用共享首选项存储响应。

然后我做了一个方法showIncentOfferwall。它应该包含一个 Url。如果响应为真,我必须在 WebView 中打开此 Url。为此,我制作了一个 WebView Activity WebviewActivity_Incent。如何在 WebView 中打开这个 Url?

我还应该做哪些更改才能正确显示它?

InitializeSDK 类:

public class InitializeSDK {
/*String json = "";
URL url;
HttpURLConnection connection = null;*/

private static String PREF_NAME = "gallectica_pref_adstuck";
private static SharedPreferences prefs;

public static void init(final Context ctx, final String developer_public_key, final String offerwall_public_key) {

    new AsyncTask<Void, Void, Boolean>() {

        protected void onPreExecute() {
            super.onPreExecute();


            prefs = ctx.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
            prefs.edit().putString("android_id", Settings.Secure.getString(ctx.getApplicationContext().getContentResolver(), Settings.Secure.ANDROID_ID)).commit();
        }

        protected Boolean doInBackground(Void... arg0) {
            //TODO: add code to read http request and store the json data in json variable
            String json = "";
            HttpURLConnection connection = null;
            InputStream is = null;

            ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("aff_id", prefs.getString("android_id", "")));
            params.add(new BasicNameValuePair("offerwall_public_key", offerwall_public_key));
            params.add(new BasicNameValuePair("developer_public_key", developer_public_key));

            try {
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost("http://a.nextput.com/apps/init/" + developer_public_key + "/a/u");//YOUR URL  ?aff_id
                httpPost.setEntity(new UrlEncodedFormEntity(params));

                HttpResponse httpResponse = httpClient.execute(httpPost);
                json = EntityUtils.toString(httpResponse.getEntity());

                JSONObject jObj = new JSONObject(json);
                boolean isSuccess = jObj.getBoolean("success");
                System.out.println("success : " + isSuccess);

                /* JSONObject jsonObject = new JSONObject(json);
                   boolean state = jsonObject.getBoolean("success");*/



                return isSuccess;
            } catch (Exception e) {
                Log.e("JSON Parser", "Error parsing data " + e.toString());
            }

            return false;
        }

        protected void onPostExecute(Boolean result) {
            super.onPostExecute(result);

            prefs.edit().putBoolean("isSuccess", result).commit();

            if (result) {
                PreferenceManager.getDefaultSharedPreferences(ctx)
                        .edit()
                        .putString("developer_public_key", developer_public_key)
                        .putString("offerwall_public_key", offerwall_public_key)
                        .apply();
            }
        }

    }.execute();
}

public static void showIncentOfferwall(final Context ctx, final String developer_public_key, final String offerwall_public_key) throws ExceptionInInitializerError {
    SharedPreferences prefs = ctx.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);

    String uri = "http://a.nextput.com/api/offerwall/" + offerwall_public_key + "/a/o";

    if (prefs.getBoolean("isSuccess", false)) {

        Intent intent = new Intent(ctx, WebviewActivity_Incent.class);
        ctx.startActivity(intent);
    } else {
        //Throw Exception
        throw new ExceptionInInitializerError("Please initialize first to show Incent Offerwalls.");
    }
}


}

WebviewActivity_Incent.java :

public class WebviewActivity_Incent extends AppCompatActivity {
private WebView webView;
private ProgressDialog dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_webview_incent);

    dialog = new ProgressDialog(this);
    dialog.setMessage("Please Wait...");

    webView = (WebView) findViewById(R.id.webView1);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.loadUrl(uri);

}

MainActivity.java :

public class MainActivity extends AppCompatActivity {
Button button1, button2;
final Context context = this;
int offerwall_id;
String offerwall_public_key;
String developer_public_key;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    InitializeSDK.init(this, developer_public_key, offerwall_public_key);

    button1 = (Button) findViewById(R.id.button1);
    button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            try {
                InitializeSDK.showIncentOfferwall(MainActivity.this);
            } catch (ExceptionInInitializerError ex) {
                System.out.println("Error==" + ex.getMessage());
            }
        }
    });

}
}

最佳答案

您可以使用 putExtra 将您的 url 传递给您的 Intent :

 Intent intent = new Intent(ctx, WebviewActivity_Incent.class);
 intent.putExtra("WEBVIEW_URL", "www.yoururl.de");
 ctx.startActivity(intent);

在您的 WebViewActivity 中:

public class WebviewActivity_Incent extends AppCompatActivity {
private WebView webView;
private ProgressDialog dialog;

/* Define uri string here */
private String uri;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_webview_incent);

    dialog = new ProgressDialog(this);
    dialog.setMessage("Please Wait...");

    webView = (WebView) findViewById(R.id.webView1);
    webView.getSettings().setJavaScriptEnabled(true);

    if(savedInstanceState != null) {
       uri = getIntent().getStringExtra("WEBVIEW_URL");
    }

    webView.loadUrl(uri);

}

关于java - 从一个类在 webview 中打开 url?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35433883/

相关文章:

java - 我正在尝试在 android 中运行前台服务,但每当我运行我的应用程序时,应用程序就会卡住并且不显示任何通知

java - Android ,无法解析 ImageDecoder

android - 从 URl Android 加载图像时出现内存泄漏

android - 使用 SqlBrite/SqlDelight(离线数据库)和 Retrofit(Http 请求)的存储库模式

Android - ImageView bottomCrop 而不是 centerCrop

python - 如何在 python 列表中创建和比较 JSON 值

javascript - 使用 $.ajax 在 PHP 中获取空数组

java - 如何在 JUnit 测试中测试集合中元素的顺序?

javascript - 在javascript中过滤嵌套的JSON数组并返回与条件匹配的内部数组

java - 通过插件 block 将 Gradle 插件应用于子项目和根项目