import org.apache.http.entity.ContentType;
import org.json.JSONException;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
public class ActOn {
static String BASE_URL = "https://restapi.actonsoftware.com/";
static String ACTON_USERNAME = "";
static String ACTON_PASSWORD = "";
static String CLIENT_ID = "";
static String CLIENT_SECRET = "";
public static void main(String[] args) throws JSONException {
try {
String access_token = getAccessToken();
Unirest.setDefaultHeader("Authorization", "Bearer " + access_token);
printListCount();
} catch (UnirestException e) {
e.printStackTrace();
}
}
private static void printListCount() throws UnirestException, JSONException {
HttpResponse < JsonNode > listResponse = Unirest.get(BASE_URL + "/api/1/list").asJson();
System.out.println("Total number of lists : " + listResponse.getBody().getObject().getInt("totalCount"));
}
private static String getAccessToken() throws UnirestException, JSONException {
HttpResponse < JsonNode > jsonResponse = Unirest.post(BASE_URL + "token")
.header("accept", "application/json")
.field("grant_type", "password")
.field("username", ACTON_USERNAME)
.field("password", ACTON_PASSWORD)
.field("client_id", CLIENT_ID)
.field("client_secret", CLIENT_SECRET)
.asJson();
return jsonResponse.getBody().getObject().getString("access_token");
}
}
此代码给出了错误;
Exception in thread "main" java.lang.NoSuchMethodError: org.apache.http.entity.ContentType.withCharset(Ljava/lang/String;)Lorg/apache/http/entity/ContentType;
at com.mashape.unirest.request.body.MultipartBody.field(MultipartBody.java:107)
at com.mashape.unirest.request.body.MultipartBody.field(MultipartBody.java:73)
at com.mashape.unirest.request.HttpRequestWithBody.field(HttpRequestWithBody.java:95)
at com.mashape.unirest.request.HttpRequestWithBody.field(HttpRequestWithBody.java:87)
at ActOn.getAccessToken(ActOn.java:48)
at ActOn.main(ActOn.java:20)
我无法理解如何解决这个问题。我添加了所有必需的jar。
答案 0 :(得分:0)
您的项目中的http-core
和unirest-java
版本不兼容。