今天在大学时我们谈了一些try
,catch
并且finally
.我对这两个例子感到困惑:
PrintWriter out = null;
try {
out = new PrintWriter(...); // We open file here
} catch (Exception e) {
e.printStackTrace();
} finally { // And we close it here
out.close();
}
Run Code Online (Sandbox Code Playgroud)
关闭文件finally
和我们只是这样做有什么区别:
PrintWriter out = null;
try {
out = new PrintWriter(...); // We open file here
} catch (Exception e) {
e.printStackTrace();
}
out.close();
Run Code Online (Sandbox Code Playgroud)
catch之后的这段代码将始终执行.
你能给我一些很好的例子来说明我们何时使用finally
以及何时将代码放入catch之后的差异?我知道最终将永远执行,但程序也会在catch块之后继续运行.
我正在尝试启用应用内结算功能.我在Google Play控制台上创建了应用内商品并获得了许可证密钥.当我尝试购买产品时.我得到这个图像错误.
这是我用来创建BillingProcessor的代码.
bp = new BillingProcessor(this, LICENSE_KEY, MERCHANT_ID, new BillingProcessor.IBillingHandler() {
@Override
public void onProductPurchased(@NonNull String productId, @Nullable TransactionDetails details) {
showToast("onProductPurchased: " + productId);
updateTextViews();
}
@Override
public void onBillingError(int errorCode, @Nullable Throwable error) {
showToast("onBillingError: " + Integer.toString(errorCode));
}
@Override
public void onBillingInitialized() {
showToast("onBillingInitialized");
readyToPurchase = true;
updateTextViews();
}
@Override
public void onPurchaseHistoryRestored() {
showToast("onPurchaseHistoryRestored");
for(String sku : bp.listOwnedProducts())
Log.d(LOG_TAG, "Owned Managed Product: " + sku);
for(String sku : bp.listOwnedSubscriptions())
Log.d(LOG_TAG, "Owned Subscription: " + sku); …
Run Code Online (Sandbox Code Playgroud) 如何覆盖组中间件?我想要实现的是为注册/登录路由添加其他节流限制。
我当前的油门是在内核中设置的。
'api' => [
'throttle:40,1',
'bindings',
],
Run Code Online (Sandbox Code Playgroud)
我想为登录/注册路由设置新的节流限制。
我就是这样做的。
Route::post('login', 'Api\UserController@login')->middleware('throttle:15,3')->name('user.login');
Route::post('register', 'Api\UserController@register')->middleware('throttle:15,3')->name('user.register');
Run Code Online (Sandbox Code Playgroud)
当我运行 php artisan route:list 时,它说这个中间件 api,throttle:15,3 应用于这条路线。
问题是当我运行登录请求时,响应头说
X-RateLimit-Limit 40
X-RateLimit-Remaining 38
Run Code Online (Sandbox Code Playgroud)
所以据我所知,我的新中间件没有被应用。但是我的油门请求被计算了两次。我如何应用不同的中间件来限制登录/注册路由并覆盖旧的?
我正在学习存储库模式,并且我看到了很多示例,其中存储库模式用于创建和更新。这是一个存储库接口的示例。
interface RepositoryInterface
{
public function all();
public function create(array $data);
public function update(array $data, $id);
public function delete($id);
public function show($id);
}
Run Code Online (Sandbox Code Playgroud)
这个存储库接口负责创建/检索和更新模型。
但是,经过一些更好的搜索,我发现人们应该避免将数据持久化在存储库中,并且存储库应该充当集合并且仅用于检索数据。这是链接。
这是他们在那里所说的话。
存储库最重要的区别可能是它们代表实体的集合。它们不代表数据库存储或缓存或任何数量的技术问题。存储库代表集合。你如何持有这些集合只是一个实现细节。
这是一个仅检索数据的存储库示例。
interface BlogRepositoryInterface
{
public function all();
public function getByUser(User $user);
}
Run Code Online (Sandbox Code Playgroud)
我想知道存储库模式的最佳实践是什么?
如果我们应该只使用存储库来检索模型,那么我们如何处理创建/更新/删除模型?
我已经尝试了来自 stackoverflow 的几个链接来获取 HmacSHA256 和与 java 一起使用的密钥,但我总是得到
func check(body: String) -> String {
let hash = body.hmac(HMACAlgorithm.sha256, key: Router.sigKey)
print("SIG: " + Router.sigKey)
print("result of hash. \(hash)")
return hash
}
Run Code Online (Sandbox Code Playgroud)
此函数从给定的字符串返回带有键的哈希值。密钥是:0393e944ee8108bb66fc9fa4f99f9c862481e9e0519e18232ba61b0767eee8c6
字符串是:示例
结果是:27effb76c97022497e25d3a5d7e823462f212a82d9ebba35f179071568b0c335
当我使用这个网站检查我的 SHA256 是否使用相同的密钥时,它返回相同的答案,所以我知道我的 swift 代码很好。但是当我尝试在 Java 中执行此操作时,这是源代码。
public static String HMAC_SHA(){
try {
String secret = "0393e944ee8108bb66fc9fa4f99f9c862481e9e0519e18232ba61b0767eee8c6";
String message = "example";
Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
SecretKeySpec secret_key = new SecretKeySpec(secret.getBytes(), "HmacSHA256");
sha256_HMAC.init(secret_key);
String hash = android.util.Base64.encodeToString(sha256_HMAC.doFinal(message.getBytes()), Base64.URL_SAFE);
return new String(Hex.encodeHex(hash.getBytes()));
}
catch (Exception e){
e.printStackTrace(); …
Run Code Online (Sandbox Code Playgroud) 您好我正在学习java编程,我刚在我的书中有任务说将int varible转换为byte变量
byte b;
int i=257;
Run Code Online (Sandbox Code Playgroud)
当我将int转换为b
b=(byte) i;
Run Code Online (Sandbox Code Playgroud)
输出是1?当字节变量的值从-128变为127时它怎么可能在我的书中他们说字节变量的有效范围是256?
为什么我必须加入f
变量f
?
float f =5.67f;
Run Code Online (Sandbox Code Playgroud)
我知道double
变量可以接受更多的抽象数字float
但是为什么我必须在我已经说过变量f
之后添加5.67
它float
?
试图获得最大的数组长度.我在网上发现最大数组长度实际上是最大整数值.
我在我的代码中使用了这段代码:
int[] array = new int[Integer.MAX_VALUE]; // 2^31-1 = 2147483647
Run Code Online (Sandbox Code Playgroud)
我得到这种错误:
Exception in thread "main" java.lang.OutOfMemoryError: Requested array size exceeds VM limit
at IntMaxValueArrayLength.main(IntMaxValueArrayLength.java:7)
Run Code Online (Sandbox Code Playgroud)
我也在互联网上发现2 ^ 31-1不能是最大值,我需要减去一些数字,我试图减去100000但仍然得到相同的错误.
我希望在我的JavaFX程序关闭时调用一个函数.我有很少的课程延伸舞台.所以我不知道我的计划将在哪个阶段结束.
因此,第一种方法是在我获得的每个阶段覆盖close函数并从那里调用我的函数.
但我想还应该有其他方式.是否有任何最终函数在我们的程序关闭之前总是被调用?所以我可以实现该功能,以便在程序关闭之前完成我需要完成的工作?
我想对所有 api 路由使用 Web 身份验证。我创建了中间件,这就是它的样子
Route::group(['middleware' => ['auth:web'], 'prefix' => 'v1',], function ($router) {
Route::apiResource('subscriptions', 'Api\SubscriptionController');
Route::post('subscriptions/{id}/resend', 'Api\SubscriptionController@resend')->name('resend');
Route::post('subscriptions/{id}/grace', 'Api\SubscriptionController@addGrace')->name('grace');
Route::apiResource('accounts', 'Api\SocialMediaAccountController');
Route::post('accounts/{id}/reset', 'Api\SocialMediaAccountController@reset');
Route::apiResource('customers', 'Api\CustomerController');
});
Run Code Online (Sandbox Code Playgroud)
当我已经登录并尝试向 api 路由发出请求时,它会将我重定向到主页。我怎样才能解决这个问题 ?
这是 config/auth.php
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'passport',
'provider' => 'users',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
], …
Run Code Online (Sandbox Code Playgroud)