3

After i update my sdk and use the API 23 in my project,i found there are some error cause cant find some related package.Then i goole it and know that api 23 has removed the apache http package.
So now what is the replacement for the old apache http package, in other word how to deal with volley in Android API 23 avoiding the errors.
I've been to the volley's google source to search the new version,but there seems no solution.

lirui
  • 1,433
  • 1
  • 12
  • 19

3 Answers3

3

Here is a Multipart request I wrote for volley https://gist.github.com/HussainDerry/0b31063b0c9dcb1cbaec. it uses OkHttp so you don't have to worry about the Apache problem anymore.

import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.RetryPolicy;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.HttpHeaderParser;

import com.squareup.okhttp.Headers;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.MultipartBuilder;
import com.squareup.okhttp.RequestBody;

import android.util.Log;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import okio.Buffer;

/**
 * Multipart request for Google's Volley using Square's OkHttp.
 * @author Hussain Al-Derry
 * @version 1.0
 * */
public class VolleyMultipartRequest extends Request<String> {

    /* Used for debugging */
    private static final String TAG = VolleyMultipartRequest.class.getSimpleName();

    /* MediaTypes */
    public static final MediaType MEDIA_TYPE_JPEG = MediaType.parse("image/jpeg");
    public static final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png");
    public static final MediaType MEDIA_TYPE_TEXT_PLAIN = MediaType.parse("text/plain");

    private MultipartBuilder mBuilder = new MultipartBuilder();
    private final Response.Listener<String> mListener;
    private RequestBody mRequestBody;

    public VolleyMultipartRequest(String url,
                                  Response.ErrorListener errorListener,
                                  Response.Listener<String> listener) {
        super(Method.POST, url, errorListener);
        mListener = listener;
        mBuilder.type(MultipartBuilder.FORM);
    }

    /**
     * Adds a collection of string values to the request.
     * @param mParams {@link HashMap} collection of values to be added to the request.
     * */
    public void addStringParams(HashMap<String, String> mParams){
        for (Map.Entry<String, String> entry : mParams.entrySet()) {
            mBuilder.addPart(
                    Headers.of("Content-Disposition", "form-data; name=\"" + entry.getKey() + "\""),
                    RequestBody.create(MEDIA_TYPE_TEXT_PLAIN, entry.getValue()));
        }
    }

    /**
     * Adds a single value to the request.
     * @param key String - the field name.
     * @param value String - the field's value.
     * */
    public void addStringParam(String key, String value) {
        mBuilder.addPart(
                Headers.of("Content-Disposition", "form-data; name=\"" + key + "\""),
                RequestBody.create(MEDIA_TYPE_TEXT_PLAIN, value));
    }

    /**
     * Adds a binary attachment to the request.
     * @param content_type {@link MediaType} - the type of the attachment.
     * @param key String - the attachment field name.
     * @param value {@link File} - the file to be attached.
     * */
    public void addAttachment(MediaType content_type, String key, File value){
        mBuilder.addPart(
                Headers.of("Content-Disposition", "form-data; name=\"" + key + "\""),
                RequestBody.create(content_type, value));
    }

    /**
     * Builds the request.
     * Must be called before adding the request to the Volley request queue.
     * */
    public void buildRequest(){
        mRequestBody = mBuilder.build();
    }

    @Override
    public String getBodyContentType() {
        return mRequestBody.contentType().toString();
    }

    @Override
    public byte[] getBody() throws AuthFailureError {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try
        {
            Buffer buffer = new Buffer();
            mRequestBody.writeTo(buffer);
            buffer.copyTo(bos);
        } catch (IOException e) {
            Log.e(TAG, e.toString());
            VolleyLog.e("IOException writing to ByteArrayOutputStream");
        }
        return bos.toByteArray();
    }

    @Override
    protected Response<String> parseNetworkResponse(NetworkResponse response) {
        String parsed;
        try {
            parsed = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
        } catch (UnsupportedEncodingException e) {
            parsed = new String(response.data);
        }
        return Response.success(parsed, HttpHeaderParser.parseCacheHeaders(response));
    }

    @Override
    public Request<?> setRetryPolicy(RetryPolicy retryPolicy) {
        return super.setRetryPolicy(retryPolicy);
    }

    @Override
    protected void deliverResponse(String response) {
        if (mListener != null) {
            mListener.onResponse(response);
        }
    }
}

I hope it's useful.

1

You can manually add the missing libraries and packages via Gradle.

For example the Apache HTTP Client can be added by adding the following line in your Gradle script under dependecies:

compile 'org.apache.httpcomponents:httpclient:4.5'

To find other packages you need I recommend the site http://mvnrepository.com/.

Steffen H
  • 116
  • 5
  • Warning:Dependency org.apache.httpcomponents:httpclient:4.5 is ignored for debug as it may be conflicting with the internal version provided by Android. – lirui Sep 02 '15 at 06:22
0

Well you could give in and in the mean time compile your app with the legacy apache library.

Inside your build.gradle for whatever is dependent on volley, insert this into the android section.

android {
    useLibrary 'org.apache.http.legacy'
}

More info on Developer Site

Other Examples

Community
  • 1
  • 1
Greg Giacovelli
  • 10,164
  • 2
  • 47
  • 64