gpt4 book ai didi

java - OkHttp异步回调中的Android SetText导致崩溃

转载 作者:搜寻专家 更新时间:2023-11-01 09:48:59 27 4
gpt4 key购买 nike

我有一个 android Activity ,它在加载 Activity 时执行异步 Okhttp 调用(从 onStart 方法调用 getActiveRequests())。

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

Button btLogout;
UserLocalStore userLocalStore;
String username;
String userEmail;
String recentAppName;
String recentRequestTime;
String isExpired;
TelephonyManager telephonyManager;
OkHttpClient client;
TextView tvRecentAppName, tvRecentRequestTime, tvIsExpired;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

userLocalStore = new UserLocalStore(this);
telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

TextView tvUserName = (TextView) findViewById(R.id.userName);
TextView tvUserEmail = (TextView) findViewById(R.id.userEmail);

tvRecentAppName = (TextView) findViewById(R.id.recentAppName);
tvRecentRequestTime = (TextView) findViewById(R.id.recentRequestTime);
tvIsExpired = (TextView) findViewById(R.id.isExpired);

client = new OkHttpClient();
username = userLocalStore.getLoggedInUser().name;
userEmail = userLocalStore.getLoggedInUser().email;

tvUserEmail.setText(userEmail);
tvUserName.setText(username);

btLogout = (Button) findViewById(R.id.btLogout);
btLogout.setOnClickListener(this);
}

@Override
protected void onStart() {
super.onStart();

if(authenticateUser() == true){
getActiveRequests();
}else{
startActivity(new Intent(this, LoginActivity.class));
}
}

我想做的是在使用 SetText 方法进行 Http 调用后更新 UI。这是我的调用,在 GetActiveRequests() 方法中实现。

client.newCall(request)
.enqueue(new Callback() {

@Override
public void onFailure(Call call, IOException e) {
String hello = "failed call";
}

@Override
public void onResponse(Call call, Response response) throws IOException {
final String responseData = response.body().string();

if (responseData != null) {
Gson gson = new Gson();
JsonElement element = gson.fromJson(responseData, JsonElement.class);
JsonObject jsonObject = element.getAsJsonObject();
final AccessRequest request = gson.fromJson(jsonObject, AccessRequest.class);

recentAppName = request.AppName.toString();
recentRequestTime = request.RequestTime.toString();
if (request.IsExpired)
isExpired = "Has Expired";
else isExpired = "Active";

tvRecentAppName.setText(recentAppName);
tvRecentRequestTime.setText(recentRequestTime);
tvIsExpired.setText(isExpired);
}
}
});

我遇到的问题是,当调试器到达 SetText 代码行时,它会导致应用程序崩溃并关闭。我不知道如何解决这个问题,但我认为它与无法更新 UI 的 Okhttp Async 调用有关,因为我的 setText 方法在 中运行良好onCreate().

最佳答案

这是因为 View 的更新只能在 UI 线程上完成,而 OkHttp 的 onResponse 在后台线程上运行。尝试像这样在主线程上运行它:

@Override
public void onResponse(Call call, Response response) throws IOException {
final String responseData = response.body().string();

if (responseData != null) {
Gson gson = new Gson();
JsonElement element = gson.fromJson(responseData, JsonElement.class);
JsonObject jsonObject = element.getAsJsonObject();
final AccessRequest request = gson.fromJson(jsonObject, AccessRequest.class);

recentAppName = request.AppName.toString();
recentRequestTime = request.RequestTime.toString();
if (request.IsExpired)
isExpired = "Has Expired";
else isExpired = "Active";

MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
//Handle UI here
tvRecentAppName.setText(recentAppName);
tvRecentRequestTime.setText(recentRequestTime);
tvIsExpired.setText(isExpired);
}
});
}
}

同样,如果您在 onFailure 上更新任何 View ,请在 UI 线程上执行。

关于java - OkHttp异步回调中的Android SetText导致崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36629346/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com