HeaderValueFormatter的静态字段INSTANC

HeaderValueFormatter的静态字段INSTANC

我收到以下错误:

Process: com.project.publiko, PID: 27061
java.lang.NoSuchFieldError: No static field INSTANCE of type Lorg/apache/http/message/BasicHeaderValueFormatter; in class Lorg/apache/http/message/BasicHeaderValueFormatter; or its superclasses (declaration of 'org.apache.http.message.BasicHeaderValueFormatter' appears in /system/framework/ext.jar)
    at org.apache.http.entity.ContentType.toString(ContentType.java:153)
    at org.apache.http.entity.mime.MultipartFormEntity.<init>(MultipartFormEntity.java:56)
    at org.apache.http.entity.mime.MultipartEntityBuilder.buildEntity(MultipartEntityBuilder.java:236)
    at org.apache.http.entity.mime.MultipartEntity.getEntity(MultipartEntity.java:119)
    at org.apache.http.entity.mime.MultipartEntity.writeTo(MultipartEntity.java:180)
    at com.project.publiko.CustomMultipartRequest.getBody(CustomMultipartRequest.java:57)
    at com.android.volley.toolbox.HurlStack.addBodyIfExists(HurlStack.java:236)
    at com.android.volley.toolbox.HurlStack.setConnectionParametersForRequest(HurlStack.java:210)
    at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:106)
    at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:93)
    at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:110)
引用错误行(等级):
android {
    useLibrary  'org.apache.http.legacy'
}
public class CustomMultipartRequest extends Request < String > {
    private Response.Listener < String > mListener = null;
    private Response.ErrorListener mEListener;

    private long cacheTimeToLive = 0;
    //
    private Map < String,
    String > header;
    MultipartEntity entity;

    @Override
    public Map < String,
    String > getHeaders() throws AuthFailureError {
        // you can add your custom headers here
        HashMap < String, String > headers = new HashMap < > ();

        return headers;
    }

    public CustomMultipartRequest(String url, Response.ErrorListener eListener, Response.Listener < String > rListener, MultipartEntity entity) {
        super(Method.POST, url, eListener);
        mListener = rListener;
        mEListener = eListener;
        this.entity = entity;
    }

    @Override
    public String getBodyContentType() {
        return entity.getContentType().getValue();
    }

    @Override
    public byte[] getBody() throws AuthFailureError {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try { **
            Error Line ** entity.writeTo(bos);
            String entityContentAsString = new String(bos.toByteArray());
            Log.e("volley", entityContentAsString);
        } catch (IOException e) {
            VolleyLog.e("IOException writing to ByteArrayOutputStream");
        }
        return bos.toByteArray();
    }

    @Override
    protected void deliverResponse(String response) {
        Log.d("DEBUG::Volley", response.toString());
        System.out.println("VolleyQueue: Response Delivered for " + getTag() +
            " (" + getSequence() + ")");
        mListener.onResponse(response);
        // TODO Auto-generated method stub
    }

    @Override
    protected Response < String > parseNetworkResponse(NetworkResponse response) {
        // TODO Auto-generated method stub
        try {

            // Volley does not handle null properly, so implement null response
            // check
            if (response.data.length == 0) {
                byte[] responseData = "{}".getBytes("UTF8");
                response = new NetworkResponse(response.statusCode,
                    responseData, response.headers, response.notModified);
            }

            String jsonString = new String(response.data,
                HttpHeaderParser.parseCharset(response.headers));
            return Response.success(jsonString,
                parseIgnoreCacheHeaders(response));
        } catch (UnsupportedEncodingException e) {
            System.out.println("VolleyQueue: Encoding Error for " + getTag() +
                " (" + getSequence() + ")");
            return Response.error(new ParseError(e));
        }
    }

    public Cache.Entry parseIgnoreCacheHeaders(NetworkResponse response) {
        long now = System.currentTimeMillis();

        Map < String, String > headers = response.headers;
        long serverDate = 0;
        String serverEtag;
        String headerValue;

        headerValue = headers.get("Date");
        if (headerValue != null) {
            serverDate = HttpHeaderParser.parseDateAsEpoch(headerValue);
        }
        serverEtag = headers.get("ETag");

        final long cacheHitButRefreshed = 15 * 60 * 1000; //Fifteen Minutes
        final long cacheExpired = cacheTimeToLive;
        final long softExpire = now + cacheHitButRefreshed;
        final long ttl = now + cacheExpired;

        Cache.Entry entry = new Cache.Entry();
        entry.data = response.data;
        entry.etag = serverEtag;
        entry.softTtl = softExpire;
        entry.ttl = ttl;
        entry.serverDate = serverDate;
        entry.responseHeaders = headers;

        return entry;
    }

    @Override
    public void deliverError(VolleyError error) {
        super.deliverError(error);
    }
}
致电类(class):
CustomMultipartRequest req = new CustomMultipartRequest(URL, //url where you want to send the multipart request
    elistener, //instance of onErrorResponse Listener
    rlistener, //instance of onResponse Listener
    entityBuilder);
req.setShouldCache(false);
RequestQueue rq = Volley.newRequestQueue(context);
rq.add(req);
您能帮我解决这个错误吗?
提前致谢 ;)

最佳答案

是库版本的错误
试试这个版本

 implementation 'org.apache.httpcomponents:httpclient:4.5.3'
    api 'org.apache.httpcomponents:httpcore:4.4.6'
    api 'org.apache.httpcomponents:httpmime:4.3.6'

关于android - 没有类型为Lorg/apache/http/message/BasicHeaderValueFormatter的静态字段INSTANCE,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36946238/

10-10 18:26