I am trying to send the image using the volley
public class ImageSendJsonObjectHeader extends JsonRequest<JSONObject> {
/* public ImageSendJsonObjectHeader (int method, String url, String requestBody, Response.Listener<JSONObject> listener, Response.ErrorListener errorListener) {
super(method, url, requestBody, listener, errorListener);
}
*/
public ImageSendJsonObjectHeader (String url, JSONObject jsonRequest, Response.Listener<JSONObject> listener,
Response.ErrorListener errorListener) {
this(jsonRequest == null ? Method.GET : Method.POST, url, jsonRequest,
listener, errorListener);
}
public ImageSendJsonObjectHeader (int method, String url, JSONObject jsonRequest,
Response.Listener<JSONObject> listener, Response.ErrorListener errorListener) {
super(method, url, (jsonRequest == null) ? null : jsonRequest.toString(), listener,
errorListener);
// Log.i("Json Object",jsonRequest.toString());
}
@Override
protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
try {
//Log.i("Response parse","Yes");
String jsonString = new String(response.data,
HttpHeaderParser.parseCharset(response.headers));
//Log.i("Json String",jsonString);
System.out.println("Json String is"+ jsonString);
//Log.i("Response Complete",response.toString());
//Log.i("Response Data",response.data.toString());
return Response.success(new JSONObject(jsonString),
HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JSONException je) {
return Response.error(new ParseError(je));
}
}
@Override
public String getBodyContentType() {
return "form-data";
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
//Log.i("Get Header","Enter");
HashMap<String, String> headers = new HashMap<String, String>();
//headers.put("Content-Type", "application/json; charset=utf-8");
//headers.put("Content-Type", "application/x-www-form-urlencoded");
Log.i("TOken is ", Constants.getTokenDB());
//System.out.println("Token Length is "+Constants.getTokenDB().length());
headers.put("x-access-token", Constants.getTokenDB());
//Log.i("Get Header","Exit");
return headers;
}
}
calling function:
private void uploadImage(File file){
//Showing the progress dialog
JSONObject jsonObject=new JSONObject();
Uri path = Uri.fromFile(file);
System.out.println("File is "+path);
//String image=getStringImage(bt);
System.out.println("Achaha "+path);
try {
jsonObject.put("Profile",path);
}catch (JSONException js){
js.printStackTrace();
}
Response.Listener listener=new Response.Listener<JSONObject>()
{
@Override
public void onResponse(JSONObject response) {
try {
Boolean responseCond=response.getBoolean("success");
System.out.println( "Response "+responseCond);
String imagePath=response.getString("url");
System.out.println("Image Path "+imagePath);
}
catch (JSONException k)
{
Log.i("On Response",k.getMessage());
k.printStackTrace();
}
}
};
String image_url= Constants.url+"upload";
imageSendJsonObjectHeader customRequest=new imageSendJsonObjectHeader(image_url,jsonObject, listener, Constants.errorListener);
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
requestQueue.add(customRequest);
}
Tried way:
File file=new File(file_path);
File file=new File(file_path); Uri path = Uri.fromFile(file);
send the bitmap
send the image as the string.
and pass the value in profile field.
Successful way using postman: