I am trying to connect to an API for getting some information in json format. I need to request the API through json encoded authorization details. Request method is Post. But I am getting error. I need to get the response in json object. I am using apache HttpClient and apache HttpCore and for Json parsing I am using json-simple-1.1.1. Please help me. As I am a novice java developer.
Here is my code
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());
}
}
getting this following error
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory at org.apache.http.conn.ssl.DefaultHostnameVerifier.(DefaultHostnameVerifier.java:70) at org.apache.http.impl.client.HttpClientBuilder.build(HttpClientBuilder.java:944) at lonexaapicon.LonexaApiCon.main(LonexaApiCon.java:33) Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 3 more