No static field INSTANCE of type Lorg/apache/http/message/BasicHeaderValueFormatter
Asked Answered
P

2

5

I get the following error:

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)

Refer error line (Gradle):

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);
    }
}

Call to the 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);

Can you help me to solve this error?

Thanks in advance ;)

Particular answered 29/4, 2016 at 19:37 Comment(5)
Possible duplicate of Getting NoSuchFieldError INSTANCE org/apache/http/message/BasicHeaderValueParserOleo
@shayanpourvatan let me try that;Particular
to use that libraries i need to remove useLibrary {apache}??..@shayanpourvatanParticular
yes you need to remove thatOleo
@shayanpourvatan getting the same error by using httpmime library and http coreParticular
A
12

Is a error of the library version try with this versions

 implementation 'org.apache.httpcomponents:httpclient:4.5.3'
    api 'org.apache.httpcomponents:httpcore:4.4.6'
    api 'org.apache.httpcomponents:httpmime:4.3.6'
Aspiration answered 19/12, 2019 at 13:58 Comment(5)
Thanks for your answer. Can someone give him an Oscar?Suicide
great answer ! are the authors of the lib aware of this ?Cosma
That's really helped, from where you find out this? any official form or documentation link about this library error?Faxon
How to inform the developers, as of now May 2021 still same error exists.Stumpf
You are a saviour!!!Boating
D
1
implementation('org.apache.httpcomponents:httpmime:4.3.6') {
        exclude module: 'httpclient'
    }
implementation 'org.apache.httpcomponents:httpclient-android:4.3.5'
Dalenedalenna answered 11/4, 2021 at 15:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.