【问题标题】:Sending JSON Post HTTP request in java [duplicate]在java中发送JSON Post HTTP请求[重复]
【发布时间】:2018-08-09 12:13:12
【问题描述】:

我正在尝试连接到 API 以获取一些 json 格式的信息。我需要通过 json 编码的授权详细信息请求 API。请求方法是 Post。但我收到错误。 我需要在 json 对象中获取响应。我正在使用 apache HttpClient 和 apache HttpCore,对于 Json 解析,我使用的是 json-simple-1.1.1。 请帮我。因为我是一个新手java开发人员。

这是我的代码

public class LonexaApiCon {

    /**
     * @param args the command line arguments
     * @throws java.io.IOException
     */
    public static void main(String[] args) throws IOException {
        // TODO code application logic here
        String postUrl = "http://myUrl.com/api/get-schedule-data";
        HttpClient httpClient = HttpClientBuilder.create().build();
        HttpPost post = new HttpPost(postUrl);

        String access_token= "myaccesstoken";
        String client_id= "myclientId";
        String client_secret= "myClientSecret";
        String username= "myuserName";
        String password= "myPassword";
        String token_type="myTokenType";


        post.setHeader("authorization", (token_type.substring(0, 1).toUpperCase() + token_type.substring(1))+" "+access_token);
        post.setHeader("Content-type", "application/json");


        JSONObject json = new JSONObject();

        json.put("grant_type","password");
        json.put("client_id",client_id);
        json.put("client_secret",client_secret);
        json.put("username",username);
        json.put("password",password);
        json.put("token_type",token_type);
        json.put("access_token",access_token);
        json.put("transport_id","72");

        post.setEntity((HttpEntity) json);
        HttpResponse  response = httpClient.execute(post);

        System.out.println(response.toString());







    }

}

收到以下错误

线程“主”java.lang.NoClassDefFoundError 中的异常: org/apache/commons/logging/LogFactory 在 org.apache.http.conn.ssl.DefaultHostnameVerifier.(DefaultHostnameVerifier.java:70) 在 org.apache.http.impl.client.HttpClientBuilder.build(HttpClientBuilder.java:944) 在 lonexaapicon.LonexaApiCon.main(LonexaApiCon.java:33) 引起: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory 在 java.net.URLClassLoader.findClass(URLClassLoader.java:381) 在 java.lang.ClassLoader.loadClass(ClassLoader.java:424) 在 sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338) 在 java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 3 更多

【问题讨论】:

  • 是否已将commons-logging.jar 添加到您的项目中?如果没有,请尝试添加它。
  • 您缺少对 Apache Commons Logging 的依赖,尝试学习使用 Maven 构建您的项目,它将大大简化依赖管理。

标签: java json api http-post


【解决方案1】:

你在使用这个依赖吗?他们为我工作。 除了代码本身,这是另一个问题

            <!-- https://mvnrepository.com/artifact/org.json/json -->
            <dependency>
                <groupId>org.json</groupId>
                <artifactId>json</artifactId>
                <version>20171018</version>
            </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.3.4</version>
        </dependency>

【讨论】:

  • 我在我的项目中使用 jar 库文件。我没有使用 Maven。
  • 使用这个版本
【解决方案2】:

将 apache HttpCore 版本更新到 4.4.3 对我有用

【讨论】:

    猜你喜欢
    • 2011-03-20
    • 2012-12-04
    • 2018-09-10
    • 2013-02-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多