从班级打开webview中的URL?

时间:2016-02-16 13:26:18

标签: java android json android-intent webview

我有一个班级InitilizeSDK我正在使用用户输入的变量developer_public_key和字符串offerwall_public_key初始化Url。我正在使用onPostExecute中的共享偏好设置存储响应。

然后我制作了一个方法showIncentOfferwall。它应该包含一个Url。如果回复为真,我必须在WebView中打开此Url。为此,我制作了WebView活动WebviewActivity_Incent。如何在WebView中打开此网址?

我应该做些什么改变来正确显示它?

InitializeSDK class:

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());
            }
        }
    });

}
}

2 个答案:

答案 0 :(得分:1)

您可以使用 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);

}

答案 1 :(得分:0)

将Url置于意图中并将其传递给WebviewActivity_Incent Activity,然后从WebviewActivity_Incent中的附加内容中获取它并在WebView中设置Url

相关问题