I try to use Volley library and upload image to server. This library should do this process in standalone mode, but have got following error message:
java.net.SocketException: sendto failed: ECONNRESET (Connection reset
by peer)
Is it maybe a server side misconfiguration?
I try to upload a jpeg image with this code:
private void uploadImage(){
final ProgressDialog loading = ProgressDialog.show(this,"Uploading...","Please wait...",false,false);
StringRequest stringRequest = new StringRequest(Request.Method.POST, UPLOAD_URL,
new Response.Listener<String>() {
#Override
public void onResponse(String s) {
loading.dismiss();
Toast.makeText(PhotoActivity.this, s , Toast.LENGTH_LONG).show();
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError volleyError) {
loading.dismiss();
Toast.makeText(PhotoActivity.this, volleyError.getMessage().toString(), Toast.LENGTH_LONG).show();
}
}){
#Override
protected Map<String, String> getParams() throws AuthFailureError {
String image = getStringImage(bitmap);
String name = editTextName.getText().toString().trim();
Map<String,String> params = new Hashtable<String, String>();
params.put(KEY_IMAGE, image);
params.put(KEY_NAME, name);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
just to be sure, you need to change your uploadImage() into something like this:
private void uploadImage(){
final ProgressDialog loading = ProgressDialog.show(this,"Uploading...","Please wait...",false,false);
//here you use your custom multi-part-request as I suggested in the comment:
ImageUploadRequest imageUploadReq = new ImageUploadRequest(UPLOAD_URL,
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError volleyError) {
loading.dismiss();
Toast.makeText(PhotoActivity.this, volleyError.getMessage().toString(), Toast.LENGTH_LONG).show();
}
},
new Response.Listener<String>() {
#Override
public void onResponse(String s) {
loading.dismiss();
Toast.makeText(PhotoActivity.this, s , Toast.LENGTH_LONG).show();
}
}, yourImageFile);
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(imageUploadReq);
}
Where your ImageUploadRequest class is defined as demonstrated in the accepted answer here like this:
public class ImageUploadRequest<T> extends Request<T> {
private static final String FILE_PART_NAME = "file";
private MultipartEntityBuilder mBuilder = MultipartEntityBuilder.create();
private final Response.Listener<T> mListener;
private final File mImageToUpload;
protected Map<String, String> headers;
public ImageUploadRequest(String uploadURL, ErrorListener errorListener, Listener<T> listener, File imageFileToUpload){
super(Method.POST, uploadURL, errorListener);
mListener = listener;
mImageToUpload = imageFileToUpload;
//call the helper method to build the multipart entity
buildMultipartEntity();
}
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = super.getHeaders();
if (headers == null || headers.equals(Collections.emptyMap())) {
headers = new HashMap<String, String>();
}
headers.put("Accept", "application/json");
return headers;
}
private void buildMultipartEntity(){
mBuilder.addBinaryBody(FILE_PART_NAME, mImageToUpload, ContentType.create("image/jpeg"), mImageToUpload.getName());
mBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
mBuilder.setLaxMode().setBoundary("xx").setCharset(Charset.forName("UTF-8"));
}
#Override
public String getBodyContentType(){
String contentTypeHeader = mBuilder.build().getContentType().getValue();
return contentTypeHeader;
}
#Override
public byte[] getBody() throws AuthFailureError{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
mBuilder.build().writeTo(bos);
} catch (IOException e) {
VolleyLog.e("IOException writing to ByteArrayOutputStream bos, building the multipart request.");
}
return bos.toByteArray();
}
#Override
protected Response<T> parseNetworkResponse(NetworkResponse response) {
T result = null;
return Response.success(result, HttpHeaderParser.parseCacheHeaders(response));
}
#Override
protected void deliverResponse(T response) {
mListener.onResponse(response);
}
}
I have made some minor adaptations of Upload an image using Google Volley to your specific situation. I hope this helps you and that others may also find it useful.
I usually get this error when I don't use url encoded queries.
Take a look URL encoding in Android
Related
I want to upload multiple images (using base64 encode).
I send these images using a for :
for(int i =1; i<6; i++){
bmp = ((BitmapDrawable)imgs[i].getDrawable()).getBitmap();
String image = getEncoded64ImageStringFromBitmap(bmp);
SendImage(image);
}
But it just send one or two requests of 5 requests! also no error occurs here. I have a requestQueue that I initialized at onCreate method.
And this is my volley request :
private void SendImage( final String image) {
String URL = APPURL;
final StringRequest stringRequest = new StringRequest(Request.Method.POST, URL,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
}) {
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new Hashtable<String, String>();
params.put("image", image);
return params;
}
};
{
requestQueue.add(stringRequest);
Toast.makeText(AddProduct.this,"added "+requestQueue.getSequenceNumber(),Toast.LENGTH_SHORT).show();
}}
You have to do this through the recursion method
like
just you have to call one time
multiFileUpload(uploadedFileCount);
then it will process all file in recursion model.
private int totalFileCount = 6;
private int uploadedFileCount = 1;
private String URL = APPURL;
private multiFileUpload(int _uploadedFileCount)
{
final StringRequest stringRequest = new StringRequest(Request.Method.POST, URL,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
if((uploadedFileCount<6)
{
uploadedFileCount++;
multiFileUpload(uploadedFileCount);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
}) {
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Bitmap bmp = ((BitmapDrawable)imgs[_uploadedFileCount].getDrawable()).getBitmap();
String image = getEncoded64ImageStringFromBitmap(bmp);
Map<String, String> params = new Hashtable<String, String>();
params.put("image", image);
return params;
}
};
{
requestQueue.add(stringRequest);
Toast.makeText(AddProduct.this,"added "+requestQueue.getSequenceNumber(),Toast.LENGTH_SHORT).show();
}
}
My problem was at the backend part. The images come at the same time and I used time() function to name them so just one or two file saved in the server.
I'm developing an Android app that communicate with a RESTful web service I wrote. Using Volley for GET methods is awesome and easy, but I can't put my finger on the POST methods.
I want to send a POST request with a String in the body of the request, and retrieve the raw response of the web service (like 200 ok, 500 server error).
All I could find is the StringRequest which doesn't allow to send with data (body), and also it bounds me to receive a parsed String response.
I also came across JsonObjectRequest which accepts data (body) but retrieve a parsed JSONObject response.
I decided to write my own implementation, but I cannot find a way to receive the raw response from the web service. How can I do it?
You can refer to the following code (of course you can customize to get more details of the network response):
try {
RequestQueue requestQueue = Volley.newRequestQueue(this);
String URL = "http://...";
JSONObject jsonBody = new JSONObject();
jsonBody.put("Title", "Android Volley Demo");
jsonBody.put("Author", "BNK");
final String requestBody = jsonBody.toString();
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.i("VOLLEY", response);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e("VOLLEY", error.toString());
}
}) {
#Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
#Override
public byte[] getBody() throws AuthFailureError {
try {
return requestBody == null ? null : requestBody.getBytes("utf-8");
} catch (UnsupportedEncodingException uee) {
VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", requestBody, "utf-8");
return null;
}
}
#Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
String responseString = "";
if (response != null) {
responseString = String.valueOf(response.statusCode);
// can get more details such as response.headers
}
return Response.success(responseString, HttpHeaderParser.parseCacheHeaders(response));
}
};
requestQueue.add(stringRequest);
} catch (JSONException e) {
e.printStackTrace();
}
I liked this one, but it is sending JSON not string as requested in the question, reposting the code here, in case the original github got removed or changed, and this one found to be useful by someone.
public static void postNewComment(Context context,final UserAccount userAccount,final String comment,final int blogId,final int postId){
mPostCommentResponse.requestStarted();
RequestQueue queue = Volley.newRequestQueue(context);
StringRequest sr = new StringRequest(Request.Method.POST,"http://api.someservice.com/post/comment", new Response.Listener<String>() {
#Override
public void onResponse(String response) {
mPostCommentResponse.requestCompleted();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
mPostCommentResponse.requestEndedWithError(error);
}
}){
#Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put("user",userAccount.getUsername());
params.put("pass",userAccount.getPassword());
params.put("comment", Uri.encode(comment));
params.put("comment_post_ID",String.valueOf(postId));
params.put("blogId",String.valueOf(blogId));
return params;
}
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String,String> params = new HashMap<String, String>();
params.put("Content-Type","application/x-www-form-urlencoded");
return params;
}
};
queue.add(sr);
}
public interface PostCommentResponseListener {
public void requestStarted();
public void requestCompleted();
public void requestEndedWithError(VolleyError error);
}
Name = editTextName.getText().toString().trim();
Email = editTextEmail.getText().toString().trim();
Phone = editTextMobile.getText().toString().trim();
JSONArray jsonArray = new JSONArray();
jsonArray.put(Name);
jsonArray.put(Email);
jsonArray.put(Phone);
final String mRequestBody = jsonArray.toString();
StringRequest stringRequest = new StringRequest(Request.Method.PUT, OTP_Url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.v("LOG_VOLLEY", response);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e("LOG_VOLLEY", error.toString());
}
}) {
#Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
#Override
public byte[] getBody() throws AuthFailureError {
try {
return mRequestBody == null ? null : mRequestBody.getBytes("utf-8");
} catch (UnsupportedEncodingException uee) {
VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", mRequestBody, "utf-8");
return null;
}
}
};
stringRequest.setShouldCache(false);
VollySupport.getmInstance(RegisterActivity.this).addToRequestque(stringRequest);
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.e("Rest response",response);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e("Rest response",error.toString());
}
}){
#Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String,String>();
params.put("name","xyz");
return params;
}
#Override
public Map<String,String> getHeaders() throws AuthFailureError {
Map<String,String> params = new HashMap<String,String>();
params.put("content-type","application/fesf");
return params;
}
};
requestQueue.add(stringRequest);
I created a function for a Volley Request. You just need to pass the arguments :
public void callvolly(final String username, final String password){
RequestQueue MyRequestQueue = Volley.newRequestQueue(this);
String url = "http://your_url.com/abc.php"; // <----enter your post url here
StringRequest MyStringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
//This code is executed if the server responds, whether or not the response contains data.
//The String 'response' contains the server's response.
}
}, new Response.ErrorListener() { //Create an error listener to handle errors appropriately.
#Override
public void onErrorResponse(VolleyError error) {
//This code is executed if there is an error.
}
}) {
protected Map<String, String> getParams() {
Map<String, String> MyData = new HashMap<String, String>();
MyData.put("username", username);
MyData.put("password", password);
return MyData;
}
};
MyRequestQueue.add(MyStringRequest);
}
Is it possible to send a simple text in the body of a StringRequest using DELETE-Method?
I couldn't find any example where somebody put something in the body of a request...
This is my request and I want to add "{'deviceid':'xyz'}" to the body (method is DELETE):
final StringRequest stringRequest = new StringRequest(method, url + "?token=" + token, new Response.Listener<String>() {
#Override
public void onResponse(String jsonResponse) {
// do something
}, new Response.ErrorListener() {
// do something
}
}) {
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("api-version", "1");
return headers;
}
};
This because Volley doesn't send the Body for DELETE by default. Only for POST, PUT and PATCH. Unfortunate to say the least
There is a workaround for it listed here: Volley - how to send DELETE request parameters?
Try this:
public class StringJSONBodyReqest extends StringRequest {
private static final String TAG = StringJSONBodyReqest.class.getName();
private final String mContent;
public StringJSONBodyReqest(int method, String url, String content, Response.Listener<String> listener, Response.ErrorListener errorListener) {
super(method, url, listener, errorListener);
mContent = content;
}
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("api-version", "1");
return headers;
}
#Override
public byte[] getBody() throws AuthFailureError {
byte[] body = new byte[0];
try {
body = mContent.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
Log.e(TAG, "Unable to gets bytes from JSON", e.fillInStackTrace());
}
return body;
}
#Override
public String getBodyContentType() {
return "application/json";
}
}
mContent is your json String
StringRequest stringRequest = new StringRequest(StringRequest.Method.PUT,
BASE_URL + "/addItem",
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.d(TAG, response);
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
//handle error
}
}) {
#Override
public byte[] getBody(){
String jsonString = json to send;
return jsonString.getBytes();
}
#Override
public String getBodyContentType() {
return "application/json";
}
};
MyRequestQueue.getInstance().addRequest(stringRequest);
I am trying to upload an image using Volley library in android but no data is being send to the server side as the file is created but it does not contain any data.
public class ProfilePicSendReq extends Request<String>
{
private MultipartEntityBuilder mBuilder = MultipartEntityBuilder.create();
private final Response.Listener<String> mListener;
private final File mImageFile;
protected Map<String, String> headers;
Context context;
public ProfilePicSendReq(Context cntxt, String url, Listener<String> listener, ErrorListener errorListener, File imageFile)
{
super(Method.POST, url, errorListener);
mListener = listener;
mImageFile = imageFile;
buildMultipartEntity();
context = cntxt;
}
#Override
public Map<String, String> getHeaders() throws AuthFailureError
{
headers = super.getHeaders();
if (headers == null
|| headers.equals(Collections.emptyMap()))
{
headers = new HashMap<String, String>();
}
headers.put("Accept", "text/plain");
return headers;
}
private void buildMultipartEntity()
{
mBuilder.addBinaryBody(mImageFile.getName(), mImageFile, ContentType.create("image/jpeg"), "profilePic");
mBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
mBuilder.setLaxMode().setBoundary("xx").setCharset(Charset.forName("UTF-8"));
}
#Override
public String getBodyContentType()
{
String contentTypeHeader = mBuilder.build().getContentType().getValue();
return contentTypeHeader;
}
#Override
public byte[] getBody() throws AuthFailureError
{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
mBuilder.build().writeTo(bos);
} catch (IOException e) {
Log.d("PicUploadMsg : ", "IOException writing to ByteArrayOutputStream bos, building the multipart request.");
}
return bos.toByteArray();
}
#Override
protected Response<String> parseNetworkResponse(NetworkResponse response)
{
String result = new String(response.data);
return Response.success(result, HttpHeaderParser.parseCacheHeaders(response));
}
#Override
protected void deliverResponse(String response)
{
mListener.onResponse(response);
}
}
It is the famous sending Mulipart data using volley i have tried several time but no data appears to leave from the client side.
I am trying to use DWorkS/VolleyPlus to make a MultiPartRequest.
I have searched through the GitHub repository, and it's sample code, but I can find no examples using this class.
I am getting a 411 response code when I make my request. My understanding is that this is sent by the server as a response when it refuses to accept a message without a content-length header. How would I go about providing this?
Below is the relevant part of the LogCat showing the error, and my method.
Looking at the code for MultiPartRequest, it has a field called isFixedStreamingMode. This name sounds similar to setFixedLengthStreamingMode for a connection, however I can't see where it ever gets used in the code. Also it is a boolean not an int, so I cannot supply a length with it. Does anyone know what this field is for?
Has anyone successfully used MultiPartRequest from this library? Any advice or examples would be greatly appreciated.
I am currently successfully making POST, GET and PUT methods using the StringRequest class from this library.
LogCat
03-11 22:17:15.388 25236-25236/au.com.xxx.yyy D/MainActivity﹕ postMyItem: http://yyy.zzz.com.au/api/v1/my_item
03-11 22:17:15.507 25236-25309/au.com.xxx.yyy E/Volley﹕ [5861] BasicNetwork.performRequest: Unexpected response code 411 for http://yyy.zzz.com.au/api/v1/my_item
03-11 22:17:15.508 25236-25236/au.com.xxx.yyy D/MainActivity﹕ error response: <html>
<head><title>411 Length Required</title></head>
<body bgcolor="white">
<center><h1>411 Length Required</h1></center>
<hr><center>nginx/1.1.19</center>
</body>
</html>
03-11 22:17:15.508 25236-25236/au.com.xxx.yyy D/MainActivity﹕ Volley Error: com.android.volley.error.VolleyError
Method
public void postMyItem(final MyItem myItem) {
String url = getString(R.string.url__server_api) + getString(R.string.post__my_item);
MultiPartRequest request = new MultiPartRequest(Request.Method.POST, url,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.d(TAG, "Volley POST MyItem response: " + response);
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, "Volley Error: " + error);
try {
String response = new String(error.networkResponse.data, "utf-8");
Log.d(TAG, "error response: " + response);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
})
{
#Override
protected Response parseNetworkResponse(NetworkResponse networkResponse) {
return null;
}
#Override
public Map<String, String> getHeaders() throws com.android.volley.error.AuthFailureError {
Map<String, String> map = new HashMap<>();
map.put(getString(R.string.header_field__access_token), mAccess_token);
return map;
}
};
if (myItem.name != null) {
request.addMultipartParam("name", "multipart/mixed", myItem.name);
}
if (myItem.quantity != null) {
request.addMultipartParam("quantity", "multipart/mixed", myItem.quantity);
}
...
//TODO: request.addFile(name, filePath)
VolleySingleton.getInstance(this).addToRequestQueue(request);
}
So sometime the servers might require content length to be told before uploading imagesetFixedStreamingMode is the method. The sample code is below
SimpleMultipartRequest request = new SimpleMultipartRequest(Method.POST, apiUrl, mListener, mErrorListener);
request.addFile("photo", image_path);
RequestQueue mRequestQueue = Volley.newRequestQueue(getApplicationContext());
mRequestQueue.add(request);
mRequestQueue.setFixedStreamingMode(true);
mRequestQueue.start();
You can use MultipartEntitiy in volley request for adding multipart data in your request body, by overriding getBody method like this:
public class MultipartRequest extends Request<String> {
private MultipartEntity entity = new MultipartEntity();
private static final String FILE_PART_NAME = "file";
private static final String STRING_PART_NAME = "text";
private static final String SELFIE_IMAGE = "selfieImage";
private static final String SELFIE_CAPTION = "cap";
private final Response.Listener<String> mListener;
private final File mFilePart;
private final String mStringPart;
public MultipartRequest(String url, Response.ErrorListener errorListener, Response.Listener<String> listener, File file, String stringPart)
{
super(Method.POST, url, errorListener);
mListener = listener;
mFilePart = file;
mStringPart = stringPart;
buildMultipartEntity();
}
private void buildMultipartEntity()
{ System.out.println("buildMultipartEntity");
entity.addPart(SELFIE_IMAGE, new FileBody(mFilePart));
try
{
entity.addPart(SELFIE_CAPTION, new StringBody(mStringPart));
}
catch (UnsupportedEncodingException e)
{
VolleyLog.e("UnsupportedEncodingException");
}
}
#Override
public String getBodyContentType()
{ System.out.println("getBodyContentType");
return entity.getContentType().getValue();
}
#Override
public byte[] getBody() throws AuthFailureError{
System.out.println("getBody");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try
{
entity.writeTo(bos);
}
catch (IOException e)
{
VolleyLog.e("IOException writing to ByteArrayOutputStream");
}
return bos.toByteArray();
}
#Override
protected Response<String> parseNetworkResponse(NetworkResponse response)
{
return Response.success("Uploaded", getCacheEntry());
}
#Override
protected void deliverResponse(String response)
{
mListener.onResponse(response);
}
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = super.getHeaders();
System.out.println("getHeaders");
if (headers == null
|| headers.equals(Collections.emptyMap())) {
headers = new HashMap<String, String>();
}
AppController.getInstance().addSessionCookie(headers);
return headers;
}
}