AsyncTask中的getStringExtra

时间:2017-12-24 23:59:06

标签: java android laravel android-studio

我有一个laravel应用程序。现在我需要将它转移到android。我的第一个任务是实施奖励视频。问题是,在触发onRewarded方法后,我需要向服务器发送一个请求,根据我的标准添加奖励。因为在Java我是新的,我遇到了一个问题。我意识到webviewHttpURLConnection有不同的会话。到目前为止,我已经得出结论,通过cookie翻译auth :: user () -> id是值得的,我设置会话,然后从前面的会话我创建cookie,从我发送到webview。下一个问题是我需要将这些cookie转移到另一个活动。我找到了Intent类,通过它传递了putExtra ("ACCESS_TOKEN", CookieValue);。问题是,我怎样才能进入第二项活动。如果我完全错误地执行此操作,我会对任何提示感到满意

private class apiPostCoins extends AsyncTask<Void,Void,Void>
{
    @Override
    protected Void doInBackground(Void... params)
    {
        try {

                //This is what i want to reach
                String cookieValue = data.getStringExtra("ACCESS_TOKEN");
                //
                URL url = new URL("http://localhost/redirect_get_coins/");

            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setRequestProperty("Accept", "application/json");

            if (conn.getResponseCode() != 200) {
                throw new RuntimeException("Failed : HTTP error code : "
                        + conn.getResponseCode());
            }
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    (conn.getInputStream())));

            String output;
            System.out.println("Output from Server .... \n");
            while ((output = br.readLine()) != null) {
                System.out.println(output);
            }
            conn.disconnect();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}

1 个答案:

答案 0 :(得分:0)

你可以这样做。

<强> FirstActivity

Intent intent = new Intent(this,SecondActivity.class);
intent.putExtra ("ACCESS_TOKEN", CookieValue);
startActivity(intent);

<强> SecondActivity

Intent intent = getIntent();
String access_token = intent.getStringExtra("ACCESS_TOKEN");