有没有人能够multipart/form-data在Android中使用Volley 完成发送POST?我没有成功尝试image/png使用POST请求上传到我们的服务器,如果有人的话,我很好奇.
我相信这样做的默认方法是public byte[] getPostBody()在Request.java类中重写并在File那里附加一个空白的Header键作为边界.然而,我的文件转换成String的Map<String, String> postParams,然后有它的编码似乎再次钝,而不是真正的优雅.我的尝试也没有成功.这实际上是阻止我们切换到这个库的唯一因素.
无论如何,所有的想法和答案都非常感激.谢谢您的帮助.
我正在开发一个与我编写的RESTful Web服务进行通信的Android应用程序.使用Volleyfor GET方法非常简单,但我不能指责POST方法.
我想在POST请求String正文中发送请求,并检索Web服务的原始响应(如200 ok,500 server error).
所有我能找到的是StringRequest不允许发送数据(正文),并且它限制我接收解析的String响应.我也遇到了JsonObjectRequest接受数据(正文)但检索解析后的JSONObject响应.
我决定编写自己的实现,但是我找不到从Web服务接收原始响应的方法.我该怎么做?
我在SO和其他内容中也经历了很多帖子.但我无法获得任何最新的官方或其他帖子,其中不包含任何弃用的代码,用于使用volley上传多个图像.我开始了解Apache HTTP Client删除和相关的新的Android M和首选使用下面的代替.
android {
useLibrary 'org.apache.http.legacy'
}
Run Code Online (Sandbox Code Playgroud)
那么,任何人都可以通过新的更新已弃用的减少排球类来帮助我进行多个图像上传吗?
我已经有一个Request子类用于http发送到服务器.问题是,我不知道如何为文件添加参数.将字符串发布到服务器很容易.但我需要将文件添加为不同的参数.我该怎么做?
public class AddNewPetRequest extends Request<JSONObject> {
private Response.Listener<JSONObject> listener;
public AddNewPetRequest(String url, Map<String, String> params,
Response.Listener<JSONObject> reponseListener, Response.ErrorListener errorListener) {
super(Request.Method.GET, url, errorListener);
this.listener = reponseListener;
this.params = params;
}
public AddNewPetRequest(int method, String url, Map<String, String> params,
Response.Listener<JSONObject> reponseListener, Response.ErrorListener errorListener) {
super(method, url, errorListener);
this.listener = reponseListener;
this.params = params;
}
protected Map<String, String> getParams()
throws com.android.volley.AuthFailureError {
return params;
};
@Override
protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
try {
String jsonString = new String(response.data,
HttpHeaderParser.parseCharset(response.headers));
return Response.success(new …Run Code Online (Sandbox Code Playgroud) 我有多个图像要在服务器上传,我有一个上传单个图像到服务器的方法.现在,我正在使用此方法通过为每个图像创建循环来发送多个图像.
有没有最快的方式将多个图像发送到服务器?提前致谢...
public int imageUpload(GroupInfoDO infoDO) {
ObjectMapper mapper = new ObjectMapper();
int groupId = 0;
try {
Bitmap bm = BitmapFactory.decodeFile(infoDO.getDpUrl());
String fileName = infoDO.getDpUrl().substring(
infoDO.getDpUrl().lastIndexOf('/') + 1,
infoDO.getDpUrl().length());
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bm.compress(CompressFormat.JPEG, 75, bos);
byte[] data = bos.toByteArray();
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost(
"http://192.168.1.24:8081/REST/groupreg/upload");
ByteArrayBody bab = new ByteArrayBody(data,
"application/octet-stream");
MultipartEntity reqEntity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("uploadFile", bab);
reqEntity.addPart("name", new StringBody(fileName));
reqEntity.addPart("grpId", new StringBody(infoDO.getGlobalAppId()
+ ""));
postRequest.setEntity(reqEntity);
HttpResponse response = httpClient.execute(postRequest); …Run Code Online (Sandbox Code Playgroud) 我正在将应用升级到org.apache.http不推荐使用的API 23 .
我当前(已弃用)的代码如下所示:
HttpClient httpClient = new DefaultHttpClient();
File file = new File(attr.Value);
String url = server_url;
HttpPost request = new HttpPost(url);
FileEntity fileEntity = new FileEntity(file, "image/png");
request.setEntity(fileEntity);
HttpResponse response = httpClient.execute(request);
String output = getContent(response.getEntity().getContent());
Run Code Online (Sandbox Code Playgroud)
我已经找到了一些关于如何使用它的建议HttpURLConnection,但它们比当前的解决方案(不能再使用)复杂得多.我正在谈论用于执行与上述相同功能的许多代码行.
有没有人有一个很好的固定更短的解决方案呢?
java android httpurlconnection apache-commons-httpclient android-6.0-marshmallow
我正在使用Volley进行API调用.我需要将图像发布到我的服务器上.
我尝试了很多MultipartRequest实现,无效.
我刚尝试使用如何通过AZ_ 在Android中使用Volley发送"multipart/form-data"POST中的示例.
但我得到MultipartRequest.getBody:IOException写入ByteArrayOutputStream错误.
你可以帮我解决我的代码,或者知道使用Volley上传图像的任何完整示例.谢谢.
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import org.apache.http.HttpEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.util.CharsetUtils;
import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyLog;
//Code by:
//https://stackoverflow.com/questions/16797468/how-to-send-a-multipart-form-data-post-in-android-with-volley
//AZ_
//
/**
* Problems : E/Volley? [17225] MultipartRequest.getBody: IOException writing to ByteArrayOutputStream
*/
public class MultipartRequest extends Request<String> {
MultipartEntityBuilder entity = MultipartEntityBuilder.create();
HttpEntity httpentity;
private String FILE_PART_NAME = …Run Code Online (Sandbox Code Playgroud) 我正在按照解决方案使用Android Volley从How to multipart数据中使用 volley发送多部分请求.但是,自SDK 22以来,httpsntity已被弃用,并且已在SDK 23上完全删除.
解决方案是使用openConnection,就像现在在Android上弃用HttpEntity一样,有什么替代方案?,但我不知道如何将它用于多部分请求
嗨我想使用标题和身体参数使用Volley向服务器发送删除请求.但我无法成功发送请求
我试过的
JSONObject jsonbObjj = new JSONObject();
try {
jsonbObjj.put("nombre", Integer.parseInt(no_of_addition
.getText().toString()));
jsonbObjj.put("cru", crue);
jsonbObjj.put("annee", 2010);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
VolleyRequest mVolleyRequest = new VolleyRequest(
Method.DELETE, url, jsonbObjj,
new Listener<JSONObject>() {
@Override
public void onResponse(JSONObject jsonObject) {
// TODO Auto-generated method stub
if (pDialog != null) {
pDialog.dismiss();
}
Log.e("Server Response", "response = "
+ jsonObject.toString());
}
}, new ErrorListener() {
@Override
public void onErrorResponse(VolleyError arg0) {
// TODO Auto-generated method stub …Run Code Online (Sandbox Code Playgroud) 我看过以下帖子: 使用Volley和没有HttpEntity工作POST多部分请求
我的问题是如何包含进度条.
进度条有解决方案,但它们都使用HttpClient,我想避免使用它,因为它已被弃用.
我只是无法弄清楚如何将两者结合起来.
编辑:
我正在寻找一个显示百分比的进度条而不仅仅是一个连续的圆圈.我想要一个带有我想要的栏的例子出现在这里,但是正在使用HTTPClient: 如何使用Volley在Android中发送"multipart/form-data"POST
我正在使用MultiPartRequester类将多部分图像上传到服务器,但我发现某些部分已弃用.例如HttpConnectionParams,getConnectionManager()等等所以任何人都有新的解决方案,不推荐使用新的API级别进行文件上传?
我正在使用此代码.
public class MultiPartRequester {
private Map<String, String> map;
private AsyncTaskCompleteListener mAsynclistener;
private int serviceCode;
private HttpClient httpclient;
private Activity activity;
private AsyncHttpRequest request;
private static final String TAG = "MultiPartRequester";
public MultiPartRequester(Activity activity, Map<String, String> map,
int serviceCode, AsyncTaskCompleteListener asyncTaskCompleteListener) {
this.map = map;
this.serviceCode = serviceCode;
this.activity = activity;
}
class AsyncHttpRequest extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
map.remove("url");
try {
HttpPost httppost = new HttpPost(urls[0]);
httpclient = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(
httpclient.getParams(), …Run Code Online (Sandbox Code Playgroud)根据此答案“ Android使用HTTP多部分表单数据将视频上传到远程服务器”,我执行了所有步骤。
但是我不知道如何为服务器端编码!我的意思是一个PHP简单的页面,可为我最忠实的敌人提供服务。
另一个问题是:YOUR_URL(以下代码段的第三行)必须是该PHP页面的地址吗?
private void uploadVideo(String videoPath) throws ParseException, IOException {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(YOUR_URL);
FileBody filebodyVideo = new FileBody(new File(videoPath));
StringBody title = new StringBody("Filename: " + videoPath);
StringBody description = new StringBody("This is a description of the video");
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("videoFile", filebodyVideo);
reqEntity.addPart("title", title);
reqEntity.addPart("description", description);
httppost.setEntity(reqEntity);
// DEBUG
System.out.println( "executing request " + httppost.getRequestLine( ) );
HttpResponse response = httpclient.execute( httppost );
HttpEntity resEntity = …Run Code Online (Sandbox Code Playgroud) android ×12
java ×3
multipart ×3
http ×2
file ×1
file-upload ×1
http-post ×1
httpentity ×1
httprequest ×1
json ×1
jsonobject ×1
php ×1
rest ×1
server ×1
sql-delete ×1
upload ×1
web-services ×1