【问题标题】:Android Studio does not find a single class of JAR at importAndroid Studio 在导入时找不到单个 JAR 类
【发布时间】:2020-03-14 19:04:16
【问题描述】:

我正在尝试导入一个 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)吗?

【问题讨论】:

    标签: java android import jar


    【解决方案1】:

    根据我的理解“HexoskinHTTPClient”不是公共类,这个类声明了默认包修改,这就是为什么这个类在包外不可见。公开课程,我认为它会起作用。

    【讨论】:

    • 你完全正确。我完全错过了这样一个事实,即这个类是唯一一个缺少 public 访问修饰符的类。
    猜你喜欢
    • 2015-03-14
    • 2015-12-17
    • 2012-01-28
    • 1970-01-01
    • 1970-01-01
    • 2015-04-12
    • 1970-01-01
    • 1970-01-01
    • 2013-09-15
    相关资源
    最近更新 更多