我正在尝试使用凌空发送图像
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;
}
}
调用函数:
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);
}
试过的方式:
答案 0 :(得分:0)
使其成为base64 String
public static String imgToBase64(Bitmap bitmap) {
ByteArrayOutputStream out = null;
try {
out = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 50, out);
out.flush();
out.close();
byte[] imgBytes = out.toByteArray();
return Base64.encodeToString(imgBytes, Base64.DEFAULT);
} catch (Exception e) {
return null;
} finally {
try {
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
答案 1 :(得分:0)
@Ankur在build.gradle中使用lagecy useLibrary&#39; org.apache.http.legacy&#39;