我正在尝试导入 jar 文件的类。
我可以导入除一个类之外的所有类。
在这里您可以看到包含我要导入的所有类的 jar:
在这里您可以看到导入,缺少 HexoskinHTTPClient
类:
类本身如下所示:
package com.companyname.hexoskin;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.companyname.hexoskin.models.HexoskinDataPoint;
import com.companyname.hexoskin.models.HexoskinRecord;
import com.companyname.hexoskin.models.HexoskinDatatype;
import com.companyname.hexoskin.models.HexoskinUnit;
import kong.unirest.Unirest;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.text.DecimalFormat;
import java.util.*;
class HexoskinHTTPClient {
private String host;
private String[] incompatibleDatatypes = {"8192", "8193", "8194", "1019", "54", "215", "281", "282", "283", "1053", "1049"};
/**
* Constructor
* @param host: The url of the API
*/
HexoskinHTTPClient(String host) {
this.host = host;
}
/**
* https://api.hexoskin.com/docs/resource/record
* @return ArrayList: List of records of type HexoskinRecord
* @throws IOException
*/
ArrayList<HexoskinRecord> getRecordsWithoutDetails() throws IOException {
// Fetch records
String response = Unirest.get(this.host + "record" + "?limit=0")
.basicAuth(HexoskinCredentials.user, HexoskinCredentials.password)
.asString()
.getBody();
// Get records array from response
ObjectMapper objectMapper = new ObjectMapper();
JsonNode records = objectMapper.readTree(response).get("objects");
// Extract relevant properties and create object
ArrayList<HexoskinRecord> hexoskinRecords = new ArrayList<>();
for(int i = 0; i < records.size(); i++) {
HexoskinRecord hexoskinRecord = objectMapper.readValue(records.get(i).toString(), HexoskinRecord.class);
hexoskinRecords.add(hexoskinRecord);
}
return hexoskinRecords;
}
/**
* https://api.hexoskin.com/docs/resource/datatype
* @return ArrayList: List of datatypes of type HexoskinDatatype
* @throws IOException
*/
ArrayList<HexoskinDatatype> getDatatypes() throws IOException, URISyntaxException {
// Fetch records
String response = Unirest.get(this.host + "datatype?limit=0")
.basicAuth(HexoskinCredentials.user, HexoskinCredentials.password)
.asString()
.getBody();
// Get datatypes array from response
ObjectMapper objectMapper = new ObjectMapper();
JsonNode datatypes = objectMapper.readTree(response).get("objects");
// Convert String Array to List
List<String> incompatibleDatatypesAsList = Arrays.asList(incompatibleDatatypes);
// Extract relevant properties and create object
ArrayList<HexoskinDatatype> hexoskinDatatypes = new ArrayList<>();
for(int i = 0; i < datatypes.size(); i++) {
// filter out incompatible types
if (!incompatibleDatatypesAsList.contains(datatypes.get(i).get("id").toString())) {
HexoskinDatatype hexoskinDatatype = objectMapper.readValue(datatypes.get(i).toString(), HexoskinDatatype.class);
// Get the unit id from unit uri
URI uri = new URI(hexoskinDatatype.getUnit());
String[] segments = uri.getPath().split("/");
String id = segments[segments.length - 1];
// Create the unit object
HexoskinUnit hexoskinUnit = this.getUnit(id);
hexoskinDatatype.setUnitObject(hexoskinUnit);
hexoskinDatatypes.add(hexoskinDatatype);
}
}
return hexoskinDatatypes;
}
/**
* https://api.hexoskin.com/docs/resource/unit
* @param unitToFetch: The id of the unit of which data should be fetched
* @return Hashmap: Map of units of type HexoskinUnit
* @throws IOException
*/
HexoskinUnit getUnit(String unitToFetch) throws IOException {
// Fetch records
String response = Unirest.get(this.host + "unit/" + unitToFetch)
.basicAuth(HexoskinCredentials.user, HexoskinCredentials.password)
.asString()
.getBody();
// Get unit from response
ObjectMapper objectMapper = new ObjectMapper();
JsonNode unit = objectMapper.readTree(response);
HexoskinUnit hexoskinUnit = objectMapper.readValue(unit.toString(), HexoskinUnit.class);
return hexoskinUnit;
}
/**
* https://api.hexoskin.com/docs/resource/data/
* @param recordId: The id of the record of which data should be fetched
* @param datatypes: The datatypes to fetch
* @return ArrayList: List of datapoints of type HexoskinDataPoint
* @throws IOException
*/
ArrayList<HexoskinDataPoint> getRecord(String recordId, ArrayList<HexoskinDatatype> datatypes) throws IOException {
String datatypesList = "";
for(int i = 0; i < datatypes.size(); i++) {
datatypesList += datatypes.get(i).getId();
if (i < datatypes.size() - 1) {
datatypesList += ",";
}
}
String response = Unirest.get(this.host + "data?datatype__in=" + datatypesList + "&record=" + recordId + "&limit=0")
.basicAuth(HexoskinCredentials.user, HexoskinCredentials.password)
.asString()
.getBody();
// Get data array from response
ObjectMapper objectMapper = new ObjectMapper();
JsonNode dataPointWrappers = objectMapper.readTree(response).get(0).get("data");
// Extract relevant properties and create object
ArrayList<HexoskinDataPoint> hexoskinDataPoints = new ArrayList<>();
for(int i = 0; i < datatypes.size(); i++) {
JsonNode dataPointWrapper = dataPointWrappers.get(datatypes.get(i).getId());
// Only create data points for entries with subentries
if (dataPointWrapper != null && dataPointWrapper.size() != 0) {
for(int j = 0; j < dataPointWrapper.size(); j++) {
HexoskinDataPoint hexoskinDataPoint = new HexoskinDataPoint(datatypes.get(i).getId());
if (dataPointWrapper.get(j).get(0) != null && !dataPointWrapper.get(j).get(0).toString().equals("null")) {
// Remove decimals which cause scientific notation strings
double secondNumber = Double.parseDouble(dataPointWrapper.get(j).get(0).toString());
DecimalFormat decimalFormat = new DecimalFormat("0");
String timestamp = decimalFormat.format(secondNumber);
hexoskinDataPoint.setTimestamp(timestamp);
} else {
hexoskinDataPoint.setTimestamp("NA");
}
if (dataPointWrapper.get(j).get(1) != null && !dataPointWrapper.get(j).get(1).toString().equals("null")) {
hexoskinDataPoint.setValue(dataPointWrapper.get(j).get(1).toString());
} else {
hexoskinDataPoint.setValue("NA");
}
hexoskinDataPoints.add(hexoskinDataPoint);
}
}
}
return hexoskinDataPoints;
}
/**
* Unirest starts a background event loop and your Java application won’t be able to exit
* until you manually shutdown all the threads.
*/
void destroy() {
Unirest.shutDown();
}
}
您知道为什么找不到一个类(HexoskinHTTPClient)吗?
最佳答案
根据我的理解,“HexoskinHTTPClient”不是公共(public)类,该类使用默认包进行了修改,这就是为什么该类在包外部不可见的原因。让类(class)公开我认为它会起作用。
关于java - Android Studio 在导入时未找到单个 JAR 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58931029/