如何处理 Android API 23 中的 volley(删除相关的 apache http 包)?
How to deal with volley in Android API 23(which remove related apache http package)?
在我更新我的 sdk 并在我的项目中使用 API 23 之后,我发现有一些错误导致无法找到一些相关的 package.Then 我去搜索并知道 api 23 删除了 apache http 包。
那么现在什么是旧的 apache http 包的替代品,换句话说如何处理 Android API 23 中的截击以避免错误。
我去volley的google source 找了新版本,好像没有解决办法。
好吧,你可以让步,同时使用旧版 apache 库编译你的应用程序。
在您的 build.gradle
中,对于任何依赖于 volley 的内容,将其插入 android 部分。
android {
useLibrary 'org.apache.http.legacy'
}
您可以通过Gradle手动添加缺少的库和包。
例如,可以通过在依赖项下的 Gradle 脚本中添加以下行来添加 Apache HTTP 客户端:
compile 'org.apache.httpcomponents:httpclient:4.5'
要查找您需要的其他软件包,我推荐网站 http://mvnrepository.com/。
这是我为 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);
}
}
}
希望有用。
在我更新我的 sdk 并在我的项目中使用 API 23 之后,我发现有一些错误导致无法找到一些相关的 package.Then 我去搜索并知道 api 23 删除了 apache http 包。
那么现在什么是旧的 apache http 包的替代品,换句话说如何处理 Android API 23 中的截击以避免错误。
我去volley的google source 找了新版本,好像没有解决办法。
好吧,你可以让步,同时使用旧版 apache 库编译你的应用程序。
在您的 build.gradle
中,对于任何依赖于 volley 的内容,将其插入 android 部分。
android {
useLibrary 'org.apache.http.legacy'
}
您可以通过Gradle手动添加缺少的库和包。
例如,可以通过在依赖项下的 Gradle 脚本中添加以下行来添加 Apache HTTP 客户端:
compile 'org.apache.httpcomponents:httpclient:4.5'
要查找您需要的其他软件包,我推荐网站 http://mvnrepository.com/。
这是我为 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);
}
}
}
希望有用。