gpt4 book ai didi

java - 从内部类中返回类型 void 的重写方法返回

转载 作者:行者123 更新时间:2023-11-29 11:23:50 24 4
gpt4 key购买 nike

我正在尝试在 Android Studio 中开发一个应用程序。我陷入了这种境地。我正在使用 AsyncHttp 客户端从 MySQL 数据库获取表名列表。我从 UI Activity 中调用以下函数来填充 Spinner 中的值。但我无法返回值。该值是在内部类和返回类型为 void 的重写方法中检索的。请查看代码以进一步理解。

public void fetchAvailableLocations ()
{
AsyncHttpClient httpClient = new AsyncHttpClient();
RequestParams requestParams = new RequestParams();

String _URL = ""; // Enter URL here

byte[] retreivedBytes;

httpClient.post(_URL, requestParams, responseHandler, new AsyncHttpResponseHandler() {


@Override
public void onSuccess(int i, cz.msebera.android.httpclient.Header[] headers, byte[] bytes) {
retrievedBytes = bytes; //This is where I'm stuck

}

@Override
public void onFailure(int i, cz.msebera.android.httpclient.Header[] headers, byte[] bytes, Throwable throwable) {
//make toasts
}
});
}
我想到的一个解决方案是在 UI Activity 中创建一个静态字段,创建一个以字节为参数的回调函数,并将静态字段分配给字节参数,然后调用它来自 onSuceed 方法。但这条路看起来很难看。有没有更简单的方法?

我对 Android 编程有点陌生,所以请帮忙。

最佳答案

不要使用继承来扩展:

public class fetchAvailableLocationsClass implements AsyncHttpResponseHandler {

public void fetchAvailableLocations() {
AsyncHttpClient httpClient = new AsyncHttpClient();
RequestParams requestParams = new RequestParams();

String _URL = ""; // Enter URL here

byte[] retreivedBytes;

httpClient.post(_URL, requestParams, responseHandler, this);


@Override
public void onSuccess ( int i, cz.msebera.android.httpclient.Header[] headers, byte[] bytes)
{
retrievedBytes = bytes; //This is where I'm stuck

}

@Override
public void onFailure ( int i, cz.msebera.android.httpclient.Header[] headers,
byte[] bytes, Throwable throwable){
//make toasts
}

}
}

关于java - 从内部类中返回类型 void 的重写方法返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38543779/

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