gpt4 book ai didi

android - 在 Android 上使用 HTTPClient 进行多个 HTTP 连接的最佳方式

转载 作者:行者123 更新时间:2023-11-30 02:49:26 25 4
gpt4 key购买 nike

我正在寻找一种同时进行多个 HTTP 连接的最佳方法。我的应用程序从 25 个不同的来源提取 RSS 提要,目前我只建立一个 HTTP 连接,这显然需要一点时间来遍历所有 URL。我在工作中使用 ASyncTask,每次下载和解析 RSS 文档时,我都会更新我的 SQLite 数据库,向其中添加提要。

private class FeedFetcherTask extends AsyncTask<String, Void, Integer> {

private Context context;
private String[] pcFeedUrls;
private String[] xboxFeedUrls;
private String[] playstationFeedUrls;
private String[] nintendoFeedUrls;
private String[] mobileFeedUrls;

private ArrayList<NewsFeed> newFeeds;

public FeedFetcherTask(Context c) {

context = c;
pcFeedUrls = c.getResources().getStringArray(R.array.pc_feeds);
xboxFeedUrls = c.getResources().getStringArray(R.array.xbox_feeds);
playstationFeedUrls = c.getResources().getStringArray(R.array.playstation_feeds);
nintendoFeedUrls = c.getResources().getStringArray(R.array.nintendo_feeds);
mobileFeedUrls = c.getResources().getStringArray(R.array.mobile_feeds);

newFeeds = new ArrayList<NewsFeed>();
}

@Override
protected Integer doInBackground(String... params) {

long time = System.currentTimeMillis();

//getActivity().setProgressBarIndeterminateVisibility(true);

getNewsForPc();
getNewsForXbox();
getNewsForPlaystation();
getNewsForNintendo();
getNewsForMobile();

newRowsInserted = storeNewsInDatabase();

long finish = System.currentTimeMillis() - time;
Log.i("time elapsed", finish / 1000.0 + "");

return null;
}

@Override
protected void onPostExecute(Integer count) {
super.onPostExecute(count);

if (getActivity() != null) {
getActivity().setProgressBarIndeterminateVisibility(false);

if (newRowsInserted == 0) {
Toast.makeText(getActivity().getApplicationContext(), getResources().getString(R.string.no_new_tweets), Toast.LENGTH_SHORT).show();
} else {
newFeedsButton.setVisibility(Button.VISIBLE);
buttonVisible = Button.VISIBLE;
newFeedsButton.setText(getResources().getQuantityString(R.plurals.new_feeds_plurals, newRowsInserted, newRowsInserted));

//TODO Create animation
}
}

else{
buttonVisible = Button.VISIBLE;
}

}

@Override
protected void onCancelled() {
super.onCancelled();
}

private void retainListViewPosition() {

int index = listView.getFirstVisiblePosition() + newFeeds.size();
View v = listView.getChildAt(0);
int top = (v == null) ? 0 : v.getTop();
listView.setSelectionFromTop(index, top);
}

private void getNewsForPc() {

int platform = NewsFeed.PLATFORM_PC;

for (String url : pcFeedUrls) {
parseRssFeed(url, platform);
}
}

private void getNewsForXbox() {

int platform = NewsFeed.PLATFORM_XBOX;

for (String url : xboxFeedUrls) {
parseRssFeed(url, platform);
}
}

private void getNewsForPlaystation() {

int platform = NewsFeed.PLATFORM_PLAYSTATION;

for (String url : playstationFeedUrls) {

parseRssFeed(url, platform);
}
}

private void getNewsForNintendo() {

int platform = NewsFeed.PLATFORM_NINTENDO;

for (String url : nintendoFeedUrls) {
parseRssFeed(url, platform);
}
}

private void getNewsForMobile() {

int platform = NewsFeed.PLATFORM_MOBILE;

for (String url : mobileFeedUrls) {
parseRssFeed(url, platform);
}
}

private void parseRssFeed(String urlIn, int platformIn) {

//TODO Too many GC, also need to close streams

try {
HttpClient apacheClient = new DefaultHttpClient();
HttpResponse response = apacheClient.execute(new HttpGet(urlIn));

BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
InputSource is = new InputSource(br);

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(is);

NodeList nodeList = document.getElementsByTagName("item");

String provider = document.getElementsByTagName("title").item(0).getTextContent();
int platform = platformIn;

Node node;
NewsFeed feed = null;
String title;
String link;
String description;
NodeList guidNodeList;
String guid;
String pubDate;
NodeList creatorNodeList;
String creator;

Element element;

for (int i = 0; i < nodeList.getLength(); i++) {

node = nodeList.item(i);

if (node.getNodeType() == Node.ELEMENT_NODE) {

element = (Element) node;
feed = new NewsFeed();

title = element.getElementsByTagName("title").item(0).getTextContent();
feed.setTitle(title);

link = element.getElementsByTagName("link").item(0).getTextContent();
feed.setLink(link);

description = element.getElementsByTagName("description").item(0).getTextContent();
feed.setDescription(description);

guidNodeList = element.getElementsByTagName("guid");

if (guidNodeList == null | guidNodeList.getLength() < 1) {
feed.setGuid(link);
} else {
guid = guidNodeList.item(0).getTextContent();
feed.setGuid(guid);
}

pubDate = element.getElementsByTagName("pubDate").item(0).getTextContent();
feed.setDate(pubDate);

creatorNodeList = element.getElementsByTagName("dc:creator");

if (creatorNodeList == null | creatorNodeList.getLength() < 1) {
feed.setCreator(provider);
} else {
creator = creatorNodeList.item(0).getTextContent();
feed.setCreator(creator);
}

feed.setProvider(provider);
feed.setPlatform(platform);

newFeeds.add(feed);
}
}
} catch (ConnectException ce) {
Toast.makeText(getActivity(), getActivity().getResources().getString(R.string.connection_exception_error), Toast.LENGTH_SHORT).show();
} catch (UnknownHostException uhe) {
uhe.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}

private int storeNewsInDatabase() {

return dao.insertAllFeeds(newFeeds);
}
}

我认为这可能是执行此操作效率最低的方法。对于如何处理此任务的任何建议,我将不胜感激。我已经看过 ExecutorService 但也不确定这是否是正确的方法。

最佳答案

我建议使用 loopj ,一个异步 HTTP 客户端库。您将需要重构您的代码以使其成为事件驱动的,但这应该是值得的。

关于android - 在 Android 上使用 HTTPClient 进行多个 HTTP 连接的最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24467634/

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