gpt4 book ai didi

java - 以 ArrayList 作为返回类型的 AsyncTask

转载 作者:行者123 更新时间:2023-11-30 05:18:24 25 4
gpt4 key购买 nike

我正在尝试从 AsyncTask 返回 ArrayList。但在主要 Activity 中我无法获取返回的数组列表。我想知道如何调用asynctask,以便我可以获得返回的arraylist。

这是 AsyncTask -

public class GetBannerDB extends AsyncTask<Void, Void, ArrayList<String>> {

GetBannerDB() {
}

@Override
protected ArrayList<String> doInBackground(Void... params) {
InputStream is = null;
ArrayList<String> bannerList = new ArrayList<String>();

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("data", "data"));

try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("url to fetch data");
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

HttpResponse response = httpClient.execute(httpPost);

HttpEntity entity = response.getEntity();

is = entity.getContent();

BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();

String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
serverResult = sb.toString().trim();

try {
String sResult = serverResult.substring(0, 7);

if (sResult.equalsIgnoreCase("success")) {
// Creating user login session
// Use user real data
String[] temp = serverResult.split("\\^");

String banner_image = temp[1];
String banner_redirect = temp[2];

bannerList.add(banner_image);
bannerList.add(banner_redirect);

} else {

}
} catch (NullPointerException ex) {

}

} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return bannerList;
}

@Override
protected void onPostExecute(ArrayList<String> result) {
super.onPostExecute(result);
mBannerTask = null;
}
}

在 MainActivity 中,我将输出称为 --

ArrayList<String> adDetails = new ArrayList<>();

mBannerTask = new GetBannerDB();
adDetails = mBannerTask.execute();

但这里我收到错误 - 类型不兼容。

我哪里出错了,请指导。

Ashok 根据您的回答,我做了以下更改 --

OnDataListener.java(作为接口(interface))

public interface OnDataListener {
void setData(ArrayList<String> result);
}

在 MainActivity.java 中 -

public class PushAds implements OnDataListener{
String serverResult = null;
private GetBannerDB mBannerTask = null;
ArrayList<String> adDetails = new ArrayList<>();

public void displayAds()
{
mBannerTask = new GetBannerDB(new OnDataListener {

public void setData(ArrayList<String> result){

adDetails = result;
}

});
mBannerTask.execute();
}
......
}

但是它给出了错误。

最佳答案

public interface OnDataListner{
void setData(ArrayList<String> result);
}

public class GetBannerDB extends AsyncTask<Void, Void, ArrayList<String>> {
public OnDataListner dataListner;

GetBannerDB(OnDataListner dataListner) {
this.dataListner = dataListner;
}

@Override
protected void onPostExecute(ArrayList<String> result) {
super.onPostExecute(result);
dataListner.setData(result);

}
}

终于

    ArrayList<String> adDetails = new ArrayList<>();

mBannerTask = new GetBannerDB(new OnDataListner{

@Overrid
public void setData(ArrayList<String> result){

adDetails = result;
//do something

}

});
mBannerTask.execute();

关于java - 以 ArrayList 作为返回类型的 AsyncTask,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59965069/

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