我有一个类InitilizeSDK
,其中使用用户输入的变量developer_public_key
和字符串offerwall_public_key
初始化Url。我将使用共享首选项将响应存储在onPostExecute
中。
然后,我做了一个方法showIncentOfferwall
。它应该包含一个网址。如果响应为true,则必须在WebView
中打开此Url。为此,我进行了WebView
活动WebviewActivity_Incent
。如何在WebView
中打开该网址?
我应该做些什么更改才能正确显示它?
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将您的网址传递给您的意图:
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);
}