在我的项目中升级SDK并使用API 23后,我发现出现了一些错误,无法找到相关的包。然后我进行了Google搜索,并了解到API 23已删除了Apache HTTP包。
那么现在旧的Apache HTTP包有什么替代品呢?换句话说,在Android API 23中如何处理Volley以避免出错。
我已经访问了Volley的Google源代码来查找新版本,但似乎没有解决方案。
在我的项目中升级SDK并使用API 23后,我发现出现了一些错误,无法找到相关的包。然后我进行了Google搜索,并了解到API 23已删除了Apache HTTP包。
那么现在旧的Apache HTTP包有什么替代品呢?换句话说,在Android API 23中如何处理Volley以避免出错。
我已经访问了Volley的Google源代码来查找新版本,但似乎没有解决方案。
这是我为Volley编写的一个多部分请求 https://gist.github.com/HussainDerry/0b31063b0c9dcb1cbaec,它使用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);
}
}
}
希望这对您有所帮助。
compile 'org.apache.httpcomponents:httpclient:4.5'
如果您需要查找其他软件包,我建议访问http://mvnrepository.com/网站。
你可以先妥协,同时使用传统的Apache库编译你的应用程序。
在与Volley相关的任何内容的build.gradle
中,在android部分插入以下内容。
android {
useLibrary 'org.apache.http.legacy'
}