post post multiPart eine gemeinsame Aufgabe. Während Httpentity in neueren API -Versionen nicht mehr verfügbar ist, gibt es immer noch Möglichkeiten, Multiprart -Anfragen mit Volley zu erreichen. Wir können Volleys Anfrageklasse wie SO erweitern:
import com.android.volley.authfailureError; import com.android.volley.networkresponse; import com.android.volley.parseerror; import com.android.volley.request; import com.android.volley.response; import com.android.volley.volleyError; import com.android.volley.toolbox.httpHeaderparser; import Java.util.map; Klasse MultipArtrequest erweitert die Anfrage
Erstellen des Multipart Body
Um den Multipart -Körper zu erstellen, können wir die folgenden Schritte verwenden:byte [] multiPartbody = new byte [ 0]; BytearrayoutputStream bos = new bytearrayoutputStream (); DataOutputStream dos = neuer DataOutputStream (BOS); versuchen { // Textdaten BuildTextPart (DOS, "Name", "John Doe"); BuildTextPart (DOS, "E -Mail", "[email protected]"); // Bilddaten Bitmap imagebitmap = bitmapfactory.decoderesource (context.getResources (), r.Drawable.my_image); BuildImagePart (DOS, "Bild", ImageBitMap); } catch (ioException e) { E. printstacktrace (); } MultiPartbody = bos.tobytearray ();
import com.android.volley.AuthFailureError; import com.android.volley.NetworkResponse; import com.android.volley.ParseError; import com.android.volley.Request; import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.toolbox.HttpHeaderParser; import java.util.Map; class MultipartRequest extends Request{ // Listener for request response private final Response.Listener mListener; // Listener for request error private final Response.ErrorListener mErrorListener; // Request headers private final Map mHeaders; // Multipart mime type (e.g., "multipart/form-data; boundary=XXX") private final String mMimeType; // Request body as multipart private final byte[] mMultipartBody; // Constructor public MultipartRequest(String url, Map headers, String mimeType, byte[] multipartBody, Response.Listener listener, Response.ErrorListener errorListener) { super(Method.POST, url, errorListener); this.mListener = listener; this.mErrorListener = errorListener; this.mHeaders = headers; this.mMimeType = mimeType; this.mMultipartBody = multipartBody; } // Getters and setters // Override getHeaders() if you need to add custom headers @Override public Map getHeaders() throws AuthFailureError { return (mHeaders != null) ? mHeaders : super.getHeaders(); } // Override getBodyContentType() to set the correct mime type for the multipart request @Override public String getBodyContentType() { return mMimeType; } // Override getBody() to return the multipart body @Override public byte[] getBody() throws AuthFailureError { return mMultipartBody; } // Override parseNetworkResponse() to parse the response as a NetworkResponse @Override protected Response parseNetworkResponse(NetworkResponse response) { try { return Response.success( response, HttpHeaderParser.parseCacheHeaders(response)); } catch (Exception e) { return Response.error(new ParseError(e)); } } // Delivering response and error @Override protected void deliverResponse(NetworkResponse response) { mListener.onResponse(response); } @Override public void deliverError(VolleyError error) { mErrorListener.onErrorResponse(error); } }
einschließlich des Multipart -Körpers in der Anfrage
Sobald der mehrteilige Körper aufgebaut ist :multipArtrequest request = new MultipArtrequest ( "my_url", NULL, // Header "Multipart/Form-Data", mehrteiler Körperbody, new response.listener
import com.android.volley.AuthFailureError; import com.android.volley.NetworkResponse; import com.android.volley.ParseError; import com.android.volley.Request; import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.toolbox.HttpHeaderParser; import java.util.Map; class MultipartRequest extends Request{ // Listener for request response private final Response.Listener mListener; // Listener for request error private final Response.ErrorListener mErrorListener; // Request headers private final Map mHeaders; // Multipart mime type (e.g., "multipart/form-data; boundary=XXX") private final String mMimeType; // Request body as multipart private final byte[] mMultipartBody; // Constructor public MultipartRequest(String url, Map headers, String mimeType, byte[] multipartBody, Response.Listener listener, Response.ErrorListener errorListener) { super(Method.POST, url, errorListener); this.mListener = listener; this.mErrorListener = errorListener; this.mHeaders = headers; this.mMimeType = mimeType; this.mMultipartBody = multipartBody; } // Getters and setters // Override getHeaders() if you need to add custom headers @Override public Map getHeaders() throws AuthFailureError { return (mHeaders != null) ? mHeaders : super.getHeaders(); } // Override getBodyContentType() to set the correct mime type for the multipart request @Override public String getBodyContentType() { return mMimeType; } // Override getBody() to return the multipart body @Override public byte[] getBody() throws AuthFailureError { return mMultipartBody; } // Override parseNetworkResponse() to parse the response as a NetworkResponse @Override protected Response parseNetworkResponse(NetworkResponse response) { try { return Response.success( response, HttpHeaderParser.parseCacheHeaders(response)); } catch (Exception e) { return Response.error(new ParseError(e)); } } // Delivering response and error @Override protected void deliverResponse(NetworkResponse response) { mListener.onResponse(response); } @Override public void deliverError(VolleyError error) { mErrorListener.onErrorResponse(error); } }
Hinzufügen von Textdaten zur Anfrage
zusätzlich zu Bilddaten können Sie auch Textdaten in Ihre Anfrage einbeziehen:privat void BuildTextPart (DataOutputStream DataOutputStream, String -Name, Stringwert) löst IOException {aus DataOutputStream.writebytes (Twohyphens Grenzlinie); DataOutputStream.writebytes ("Inhaltsdisposition: Form-Data; name = \" name "\" "lineend); DataOutputStream.writebytes (Lineend); DataOutputStream.writebytes (Wert); DataOutputStream.writebytes (Lineend); }
import com.android.volley.AuthFailureError; import com.android.volley.NetworkResponse; import com.android.volley.ParseError; import com.android.volley.Request; import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.toolbox.HttpHeaderParser; import java.util.Map; class MultipartRequest extends RequestErsetzen Sie Twohyphens, Grenary und Lineend mit entsprechenden Werten aus Ihrem Code.{ // Listener for request response private final Response.Listener mListener; // Listener for request error private final Response.ErrorListener mErrorListener; // Request headers private final Map mHeaders; // Multipart mime type (e.g., "multipart/form-data; boundary=XXX") private final String mMimeType; // Request body as multipart private final byte[] mMultipartBody; // Constructor public MultipartRequest(String url, Map headers, String mimeType, byte[] multipartBody, Response.Listener listener, Response.ErrorListener errorListener) { super(Method.POST, url, errorListener); this.mListener = listener; this.mErrorListener = errorListener; this.mHeaders = headers; this.mMimeType = mimeType; this.mMultipartBody = multipartBody; } // Getters and setters // Override getHeaders() if you need to add custom headers @Override public Map getHeaders() throws AuthFailureError { return (mHeaders != null) ? mHeaders : super.getHeaders(); } // Override getBodyContentType() to set the correct mime type for the multipart request @Override public String getBodyContentType() { return mMimeType; } // Override getBody() to return the multipart body @Override public byte[] getBody() throws AuthFailureError { return mMultipartBody; } // Override parseNetworkResponse() to parse the response as a NetworkResponse @Override protected Response parseNetworkResponse(NetworkResponse response) { try { return Response.success( response, HttpHeaderParser.parseCacheHeaders(response)); } catch (Exception e) { return Response.error(new ParseError(e)); } } // Delivering response and error @Override protected void deliverResponse(NetworkResponse response) { mListener.onResponse(response); } @Override public void deliverError(VolleyError error) { mErrorListener.onErrorResponse(error); } }
komplettes Code sample
Das folgende Code -Beispiel zeigt eine vollständige modulare Implementierung einer volleyltipArtrequest -Klasse:
import com.android.volley.AuthFailureError; import com.android.volley.NetworkResponse; import com.android.volley.ParseError; import com.android.volley.Request; import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.toolbox.HttpHeaderParser; import java.util.Map; class MultipartRequest extends Requestimport com.android.volley.authfailureError; import com.android.volley.networkresponse; import com.android.volley.parseerror; import com.android.volley.request; import com.android.volley.response; import com.android.volley.volleyError; import com.android.volley.toolbox.httpHeaderparser; importieren java.io.BytearrayoutputStream; importieren java.io.dataoutputStream; importieren java.io.ioException; import Java.util.hashMap; import Java.util.map; Öffentliche Klasse VolleyultipArtrequest erweitert die Anfrage{ // Listener for request response private final Response.Listener mListener; // Listener for request error private final Response.ErrorListener mErrorListener; // Request headers private final Map mHeaders; // Multipart mime type (e.g., "multipart/form-data; boundary=XXX") private final String mMimeType; // Request body as multipart private final byte[] mMultipartBody; // Constructor public MultipartRequest(String url, Map headers, String mimeType, byte[] multipartBody, Response.Listener listener, Response.ErrorListener errorListener) { super(Method.POST, url, errorListener); this.mListener = listener; this.mErrorListener = errorListener; this.mHeaders = headers; this.mMimeType = mimeType; this.mMultipartBody = multipartBody; } // Getters and setters // Override getHeaders() if you need to add custom headers @Override public Map getHeaders() throws AuthFailureError { return (mHeaders != null) ? mHeaders : super.getHeaders(); } // Override getBodyContentType() to set the correct mime type for the multipart request @Override public String getBodyContentType() { return mMimeType; } // Override getBody() to return the multipart body @Override public byte[] getBody() throws AuthFailureError { return mMultipartBody; } // Override parseNetworkResponse() to parse the response as a NetworkResponse @Override protected Response parseNetworkResponse(NetworkResponse response) { try { return Response.success( response, HttpHeaderParser.parseCacheHeaders(response)); } catch (Exception e) { return Response.error(new ParseError(e)); } } // Delivering response and error @Override protected void deliverResponse(NetworkResponse response) { mListener.onResponse(response); } @Override public void deliverError(VolleyError error) { mErrorListener.onErrorResponse(error); } }
Haftungsausschluss: Alle bereitgestellten Ressourcen stammen teilweise aus dem Internet. Wenn eine Verletzung Ihres Urheberrechts oder anderer Rechte und Interessen vorliegt, erläutern Sie bitte die detaillierten Gründe und legen Sie einen Nachweis des Urheberrechts oder Ihrer Rechte und Interessen vor und senden Sie ihn dann an die E-Mail-Adresse: [email protected] Wir werden die Angelegenheit so schnell wie möglich für Sie erledigen.
Copyright© 2022 湘ICP备2022001581号-3