如何在Android API 23中处理凌空(删除相关的apache http包)?

时间:2015-09-02 05:53:15

标签: android android-volley

在我更新我的sdk并在我的项目中使用API​​ 23之后,我发现有一些错误导致无法找到一些相关的包。然后我goole它并且知道api 23已经删除了apache http包。
那么现在什么是旧的apache http包的替代品,换句话说如何处理Android API 23中的凌空,避免错误。
我已经去过凌空的google source 来搜索新版本,但似乎没有解决方案。

3 个答案:

答案 0 :(得分:3)

这是我为volley https://gist.github.com/HussainDerry/0b31063b0c9dcb1cbaec写的一个Multipart请求。 它使用OkHttp,因此您不必再担心Apache问题了。

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

我希望它有用。

答案 1 :(得分:1)

您可以通过Gradle手动添加缺少的库和包。

例如,可以通过在依赖项下的Gradle脚本中添加以下行来添加Apache HTTP Client:

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

要查找您需要的其他套餐,我建议您使用网站http://mvnrepository.com/

答案 2 :(得分:0)

您可以放弃并同时使用旧版apache库编译您的应用程序。

在你的build.gradle内,对于任何依赖于凌空的内容,请将其插入到android部分。

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

More info on Developer Site

Other Examples