JAVA样例

public void javaDemo(){
    String apiUrl = api接口url;
    Gson gson = new Gson();
    HttpClient client = null;
    PostMethod method = null;
    try{
        client = new HttpClient();
        client.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
        method = new PostMethod(apiUrl);
        //传参
        String paras = "{CompID: 1, SecretKey: e10adc3949ba59abbe56e057f20f883e, RequestId: UUID(每次请求唯一)}";
        RequestEntity entity = new StringRequestEntity(paras,"application/json","UTF-8");
        method.setRequestEntity(entity);
        method.addRequestHeader("Content-Type","applicrgbation/json;charset=utf-8");
        int code = client.executeMethod(method);
        if(code!=HttpStatus.SC_OK){
            throw new Exception("");
        }
        String respText = method.getResponseBodyAsString();
        //返回的数据
        Map<String, Object> map = gson.fromJson(respText, Map.class);
    }catch(Exception e){
        e.printStackTrace();
    }finally{
        if(method!=null){
            method.releaseConnection();
        }
        if(client!=null){
            client.getHttpConnectionManager().closeIdleConnections(1);
        }
    }

}
© HeDonghai all right reserved,powered by Gitbook文件修订时间: 2018-01-31 16:17:14

results matching ""

    No results matching ""