首页 > 解决方案 > Java:解决未经检查的调用

问题描述

即使我已经在我的方法中引入了类型参数,我也遇到了未经检查的调用的问题。我不想压制警告,因为我很确定有办法以某种方式防止这种情况。

我的主要课程(这给了我警告)

HttpUtil.fetch(
        url,
        headers,
        HttpRequest.Method.POST,
        new HttpRequest.Form.Builder(HttpRequest.Form.Encoding.URL_ENCODED)
                .set("password", iLoginPass.getText().toString())
                .set("login", iLoginUser.getText().toString())
                .build()
)
.then(HttpResponse::getBody)
.then((Promise.Resolver<Void, Document>) value -> {
    final Elements elems = value.body().getElementsByTag("table");
    Toast.makeText(MainActivity.this, elems.text(), Toast.LENGTH_LONG).show();
    return null;
});

我的承诺课

// T: input, U: progress (unused but just in case), V: output (resolved value)
public class Promise<T, U, V> {
    // RV: new return value (aka. resolved value)
    public <RV> Promise then(final Resolver<RV, V> nextResolver){
        return new Promise<V, U, RV>(nextResolver, this.rejector, resolvedValue);
    }
}

Also ignore the HttpUtil.fetch function because it returns a Promise object with parameter types <String, Void, HttpResponse>. The body should return another object from Document class which shouldn't be a problem because if I did .<Document>then(...) it tells me 'the parameterized type is redundant' and wanted me to infer the type. Also, the important thing to note here is the warning pop out only if I did with two or more .then(...)

标签: javaandroid

解决方案


没关系。我知道如何解决这个问题。

.then(...)我的Promise课堂上我应该做

public class Promise<T, U, V> {
    public <RV> Promise<V, U, RV> then(final Resolver<RV, V> nextResolver){
        return new Promise<V, U, RV>(nextResolver, this.rejector, resolvedValue);
    }
}

警告就这样消失了


推荐阅读