没有静态字段INSTANCE类型为Lorg / apache / http / message / BasicHeaderValueFormatter

时间:2016-04-29 19:37:35

标签: android apache httpclient android-volley multipartentity

错误日志;

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)

尝试了所有可能的方法,没有得到正确的解决方案 自从最近5个小时以来坚持这个错误 尝试了所有谷歌的答案 我应该编译哪个库????

*参考ErrorLine

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

}

}

致电课程

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

提前感谢;)

2 个答案:

答案 0 :(得分:1)

是库版本的错误 尝试使用此版本

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

答案 1 :(得分:0)

implementation('org.apache.httpcomponents:httpmime:4.3.6') {
        exclude module: 'httpclient'
    }
implementation 'org.apache.httpcomponents:httpclient-android:4.3.5'