当 android 版本是 4.1.2 时,这段代码运行良好。 但是4.2.0之后,代码不行了 这是我下面的代码
import androidx.appcompat.app.AppCompatActivity;
import java.io.IOException;
import java.util.UUID;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import android.os.Bundle;
import android.widget.TextView;
import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String accessKey = ("code erased");//api key 1
String secretKey = ("code erased");//api key 2
String serverUrl = ("https://api.upbit.com");
Algorithm algorithm = Algorithm.HMAC256(secretKey);
String jwtToken = JWT.create()
.withClaim("access_key", accessKey)
.withClaim("nonce", UUID.randomUUID().toString())
.sign(algorithm);
String authenticationToken = "Bearer " + jwtToken;
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(serverUrl + "/v1/accounts");
request.setHeader("Content-Type", "application/json");
request.addHeader("Authorization", authenticationToken);
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
//System.out.println(EntityUtils.toString(entity, "UTF-8"));
double balance_total = 0.0;
JsonParser jsonParser = new JsonParser();
JsonArray jsonArray = (JsonArray) jsonParser.parse(EntityUtils.toString(entity, "UTF-8"));
for (int i = 0; i < jsonArray.size(); i++) {
JsonObject object = (JsonObject) jsonArray.get(i);
Double balances = object.get("balance").getAsDouble();
balance_total += balances;
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
我的依赖是
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
compileOnly 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'
implementation 'com.fasterxml.jackson.core:jackson-databind:2.3.2'
implementation 'com.auth0.android:jwtdecode:2.0.0'
api 'io.jsonwebtoken:jjwt-api:0.11.2'
runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.11.2'
runtimeOnly('io.jsonwebtoken:jjwt-orgjson:0.11.2') { exclude group: 'org.json', module: 'json'}
runtimeOnly 'org.bouncycastle:bcprov-jdk15on:1.60'
implementation 'com.android.support:multidex:1.0.3'
implementation 'com.auth0:java-jwt:3.15.0'
implementation 'org.apache.httpcomponents:httpclient-android:4.3.5.1'
implementation 'com.google.code.gson:gson:2.8.6'
androidTestImplementation "com.github.tomakehurst:wiremock-standalone:2.26.3"
我使用 commons-codes 1.10v 作为 jar 文件而不是实现,因为我使用了两个 api,而其他 api 不适用于 commons-codec 实现。(如果我使用 jar 文件和依赖项,则会出现重复的类两者)
出现这样的错误
W/pclass.apitest: Accessing hidden method Ljava/lang/Thread;-><clinit>()V (blacklist, linking, denied)
Accessing hidden field Ljava/lang/Thread;->EMPTY_STACK_TRACE:[Ljava/lang/StackTraceElement; (greylist-max-o, linking, denied)
Accessing hidden field Ljava/lang/Thread;->SUBCLASS_IMPLEMENTATION_PERMISSION:Ljava/lang/RuntimePermission; (greylist-max-o, linking, denied)
Accessing hidden field Ljava/lang/Thread;->lock:Ljava/lang/Object; (greylist, linking, allowed)
W/pclass.apitest: Accessing hidden field Ljava/lang/Thread;->daemon:Z (greylist, linking, allowed)
Accessing hidden field Ljava/lang/Thread;->stillborn:Z (greylist-max-o, linking, denied)
Accessing hidden field Ljava/lang/Thread;->threadLocals:Ljava/lang/ThreadLocal$ThreadLocalMap; (greylist, linking, allowed)
Accessing hidden field Ljava/lang/Thread;->inheritableThreadLocals:Ljava/lang/ThreadLocal$ThreadLocalMap; (greylist, linking, allowed)
Accessing hidden field Ljava/lang/Thread;->systemDaemon:Z (blacklist, linking, denied)
Accessing hidden field Ljava/lang/Thread;->started:Z (greylist-max-o, linking, denied)
W/pclass.apitest: Accessing hidden field Ljava/lang/Thread;->blockerLock:Ljava/lang/Object; (greylist-max-o, linking, denied)
Accessing hidden method Ljava/lang/Thread;->nextThreadNum()I (greylist-max-o, linking, denied)
Accessing hidden field Ljava/lang/Thread;->stillborn:Z (greylist-max-o, linking, denied)
Accessing hidden field Ljava/lang/Thread;->systemDaemon:Z (blacklist, linking, denied)
Accessing hidden field Ljava/lang/Thread;->started:Z (greylist-max-o, linking, denied)
Accessing hidden field Ljava/lang/Thread;->blockerLock:Ljava/lang/Object; (greylist-max-o, linking, denied)
Accessing hidden method Ljava/lang/Thread;->nextThreadNum()I (greylist-max-o, linking, denied)
Accessing hidden field Ljava/lang/Thread;->stillborn:Z (greylist-max-o, linking, denied)
Accessing hidden field Ljava/lang/Thread;->systemDaemon:Z (blacklist, linking, denied)
Accessing hidden field Ljava/lang/Thread;->started:Z (greylist-max-o, linking, denied)
Accessing hidden field Ljava/lang/Thread;->blockerLock:Ljava/lang/Object; (greylist-max-o, linking, denied)
.
.
.
出现大量“访问隐藏字段或方法”错误
安卓更新有问题吗 还是我做错了什么?
答案 0 :(得分:0)
您正在主线程上执行网络请求,这是一个阻塞进程,android 不允许在主线程上执行网络请求,您应该在不会阻塞 UI 处理的后台线程上执行它。
>