如何通过OkHttp将查询参数添加到HTTP GET请求?

编程入门 行业动态 更新时间:2024-10-09 15:24:59
本文介绍了如何通过OkHttp将查询参数添加到HTTP GET请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用最新的okhttp版本: okhttp-2.3.0.jar

如何在okhttp中向GET请求添加查询参数在java?

我发现

I am using the latest okhttp version: okhttp-2.3.0.jar

How to add query parameters to GET request in okhttp in java ?

I found a related question about android, but no answer here!

解决方案

As mentioned in the other answer, okhttp v2.4 offers new functionality that does make this possible.

See square.github.io/okhttp/2.x/okhttp/com/squareup/okhttp/HttpUrl.Builder.html#addQueryParameter-java.lang.String-java.lang.String-

This is not possible with the current version of okhttp, there is no method provided that will handle this for you.

The next best thing is building an url string or an URL object (found in java.URL) with the query included yourself, and pass that to the request builder of okhttp.

As you can see, the Request.Builder can take either a String or an URL.

Examples on how to build an url can be found at What is the idiomatic way to compose a URL or URI in Java?

更多推荐

如何通过OkHttp将查询参数添加到HTTP GET请求?

本文发布于:2023-11-25 10:20:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1629371.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:参数   OkHttp   HTTP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!