KiteRequest.java 文件源码

java
阅读 18 收藏 0 点赞 0 评论 0

项目:javakiteconnect 作者:
/**
 * POST request using UniRest library.
 *
 * @param url is endpoint to which request has to be done.
 * @param params is data that has to be sent in body.
 * @return JSONObject returns json response.
 * @throws KiteException contains error message and error code inside.
 * @throws JSONException occurs when there is error while parsing data.
 */

public JSONObject postRequest(String url, Map<String, Object> params) throws KiteException, JSONException {
    try {
        if(KiteConnect.httpHost != null){
            Unirest.setProxy(KiteConnect.httpHost);
        }
        HttpResponse<JsonNode> response = Unirest.post(url)
                .header("accept", "application/json")
                .fields(params)
                .asJson();

        JsonNode body = response.getBody();
        JSONObject jsonObject = body.getObject();
        int code = response.getStatus();

        if (code == 200)
            return jsonObject;
        else
            throw dealWithKiteException(body, code);

    } catch (UnirestException e) {
       throw new KiteNoNetworkException("Connection error");
    }

}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号