java - 向 android 中的 URL 列表发出多个 get 请求

标签 java android python

我基本上有一个字符串列表(大约 20 到 60 个字符串),我想发送一个 post 请求(并获取 json 格式的结果,并将响应中的特定值附加到新列表)列表中的项目。我想将 ThreadPoolExecutor 与特定数量的工作人员一起使用。

我尝试了一些事情,但不幸的是我做不到。我可以一次执行一个请求,但这效率不高并且需要很长时间。

我在 python 中有这段代码,它完全实现了我想要做的事情,但不幸的是我无法在 Java 中重现它。

#This function makes a single request using one string from the list
def getname(id):
        url = "https://api-public-service.battledash.co/fortnite/cosmetics/search/id?q=" + id
    with requests.session() as ss:
        l = ss.get(url).json()
#we return a value from the response

    return(l['readableName'])




def getnamelist(ids):
    result = []
    ids = deque(ids)
#Turning the list into a dict and back to list in order to remove duplicated items
    ids = list(dict.fromkeys(ids))
#max workers is set to 10
    with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
#running the getname function which sends a single request and return a name from id
        results = executor.map(getname,ids)
#appending all results into a list
    for it in tuple(results):
        result.append(it)

    return(result)

最佳答案

注意:由于您没有指定 Java 版本,所以我的答案针对的是 Java >= 8。

假设您有以下线程安全函数:public SomeHttpResponseType PerformGetRequest(String url):

public List<SomeHttpResponseType> performGetRequests(List<String> urls) {
   return urls.stream().parallelStream()
       .map(this::performGetRequest)
       .collect(Collectors.toList())
}

这使用默认的 ForkJoinPool。如果您想指定自己的线程池,请尝试以下操作:

ForkJoinPool forkJoinPool = null;

    try {
        forkJoinPool = new ForkJoinPool(parallelism);
        forkJoinPool.submit(() ->
           urls.stream().parallelStream()
           .map(this::performGetRequest)
           .collect(Collectors.toList())
        ).get()
    } catch(InterruptedException | ExecutionException e) {
        e.printStackTrace();
    } finally {
        if (forkJoinPool != null) {
            forkJoinPool.shutdown(); //always remember to shutdown the pool
        }
    }

(https://www.codementor.io/nitinpuri/controlling-parallelism-of-java-8-collection-streams-umex0qbt1的改编版本)

关于java - 向 android 中的 URL 列表发出多个 get 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56299738/

相关文章:

python - 通过多级字典在 Pandas 中创建面板

安卓|在 Genymotion 模拟器上调试 native 应用程序

c# - 使用 Xamarin Android 连接到蓝牙扫描仪

java - 安卓 : Refresh ListView not working

java - Selenium:元素定义的优化(页面对象)

python - pandas DataFrame的条件分割

python - 提取一些可选的标记

java - 在 Java 中读取 token

java - 如何将 Pixmap/Texture 加载到 AssetManager 中?

java - 在logstash中使用jdbc输入插件时的时间变化