相关疑难解决方法(0)

使用Java发送HTTP Post Payload

我正在尝试连接到groovehark API,这是http请求

POST URL
http://api.grooveshark.com/ws3.php?sig=f699614eba23b4b528cb830305a9fc77
POST payload
{"method":'addUserFavoriteSong",'parameters":{"songID":30547543},"header": 
{"wsKey":'key","sessionID":'df8fec35811a6b240808563d9f72fa2'}}
Run Code Online (Sandbox Code Playgroud)

我的问题是如何通过Java发送此请求?

java httprequest payload grooveshark

5
推荐指数
1
解决办法
3万
查看次数

当唯一部分位于循环/ try-catch中时,避免重复代码

我有一个类有两个方法,有很多重复的代码但是唯一的位在整个事件的中间.根据我的研究,我认为我应该做"执行周围方法"模式,但我找不到我可以遵循的资源,因为他们似乎都使用我无法复制的代码.

我有两种方法,apiPost和apiGet,我已粘贴在下面.我已经将这些方法的独特部分包装在一起,注释显示了唯一部分的开始和结束位置:

/**
 * Class that handles authorising the connection and handles posting and getting data
 *
 * @version     %I%, %G%
 * @since       1.0
 */
public class CallHandler {
    private static PropertyLoader props = PropertyLoader.getInstance();
    final static int MAX = props.getPropertyAsInteger(props.MAX_REQUESTS);
    private final Logger log = LoggerFactory.getLogger(CallHandler.class);
    private final static String POST = "POST";
    private final static String GET = "GET";

    /**
     * Makes a POST call to the API URL provided and returns the JSON response as a string …
Run Code Online (Sandbox Code Playgroud)

java code-duplication

4
推荐指数
1
解决办法
91
查看次数

需要 Jersey 客户端 api 方式来发布带有 json 有效负载和标头的 web 请求

我正在使用 jersey(org.glassfish.jersey.client.*) 为我的 REST API 之一编写客户端。

api 网址是:http://localhost:5676/searchws/search/getresults (POST)

这个 api 返回一个 json 响应。我需要使用 jersey 客户端提供有效负载,这就是我被卡住的地方。以下是我需要提供的有效载荷的示例提取物(最好是字符串)

问题是我如何将有效负载(XML/JSON)作为字符串或实体提供给我的 webtarget。

我看到了 calden 提到的提供有效负载的答案How to send Request payload to REST API in java? 但我正在寻找一种在球衣客户端中做到这一点的方法。

这是我到目前为止的代码,它不能完全用于发布请求。

public class RequestGenerator 
{

    private WebTarget target;
    private ClientConfig config;
    private Client client;
    private Response response;

    public RequestGenerator(Method RequestSendingMethod) throws Exception
    {
        switch (RequestSendingMethod)
        {
            case POST :
                config = new ClientConfig();
                client = ClientBuilder.newClient(config);
                target = client.target("http://localhost:5676/searchws").path("search").path("getresults");
                String payload = "{\"query\":\"(filter:(\\\"google\\\")) AND (count_options_availbale:[1 TO *])\"}"; …
Run Code Online (Sandbox Code Playgroud)

java rest json jersey-client

1
推荐指数
1
解决办法
3万
查看次数