How to send multiple base64 images on server in android

I am developing an application in which I need to send several images to a server in base64 format by calling Restful Web Service.

Code for calling Restful web service:

try { //instantiates httpclient to make request DefaultHttpClient httpclient = new DefaultHttpClient(); //url with the post data HttpPost request = new HttpPost(urlPath); //convert parameters into JSON object JSONObject holder = new JSONObject(jsonObjString); //passes the results to a string builder/entity StringEntity se = new StringEntity(holder.toString()); //sets the post request as the resulting string request.setEntity(se); //sets a request header so the page receving the request //will know what to do with it request.setHeader("Accept", "application/json"); request.setHeader("Content-type", "application/json"); //Handles what is returned from the page ResponseHandler<String> responseHandler = new BasicResponseHandler(); responseString = httpclient.execute(request, responseHandler); }catch (Exception e) { e.printStackTrace(); } 

Another problem is that when I call the web service with a JSON object as the request parameter, I get ThreadPoolExecutor. How can we solve this problem. Is there an ideal way to upload multiple base64 images to the server by calling the Restful Web Service.

+7
android
source share
2 answers

I also had the same problem, but after some research I sort it out. You cannot post multiple images in json through a soothing web service.

To do this, you must use the xml format and click on the service.

1) First create an xml request:

 public String createXMLRequestForMultiplePhoto() { StringBuffer strBuffer = null; try { strBuffer = new StringBuffer(); strBuffer.append("<?xml version='1.0' encoding='utf-8'?><UploadChallanMultiplePhoto>"); strBuffer.append("<userid>" + Constant.USER_ID + "</userid>"); strBuffer.append("<accesstoken>" + Constant.ACCESS_TOCKEN + "</accesstoken>"); strBuffer.append("<TempChallanNo>" + "0" + "</TempChallanNo>"); //ADD MULTIPLE PHOTO TAGS START strBuffer.append("<driverphotos>"); int i=0; while(i<hexStringArrayList.size()){ strBuffer.append("<driverphoto>"); strBuffer.append(hexStringArrayList.get(i)); strBuffer.append("</driverphoto>"); i++; } strBuffer.append("</driverphotos>"); //ADD MULTIPLE PHOTO TAGS ENDS strBuffer.append("</UploadChallanMultiplePhoto>"); } catch (Exception e) { } return strBuffer.toString(); } 

2) Now you can use the code below to get into the web service:

 public static String fetchAllActivatedRestForAddMultiplPhoto(String url) { String responseString = ""; try { int TIMEOUT_MILLISEC = 20000;//100000 milisec = 10 seconds int SOCKET_TIMEOUT_MILISEC=20000; HttpParams httpParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParams,TIMEOUT_MILLISEC); HttpConnectionParams.setSoTimeout(httpParams, SOCKET_TIMEOUT_MILISEC); HttpClient client = new DefaultHttpClient(httpParams); HttpPost request = new HttpPost(url); ByteArrayEntity entity = new ByteArrayEntity(Constant.addChallanXml.getBytes()); request.setEntity(entity); HttpResponse response = client.execute(request); responseString = request(response); // here you get the response System.out.println(responseString); // this line will print the /*if (response != null && response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { HttpEntity entity1 = response.getEntity(); responseString=EntityUtils.toString(entity1); }*/ // response on Console } catch (Exception exception) { exception.printStackTrace(); } return responseString; } 

Hope this helps you.

+5
source share

here is the code you download using mulitpart. It will load your base64 image.

  String url = "xyz";//url here. File file = new File("image path on harddrive");//make a file of image. MultipartEntity mpEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); mpEntity.addPart("content", new FileBody(file, "application/octet")); HttpPost httppost = new HttpPost( url); httppost.setEntity(mpEntity); DefaultHttpClient httpclient = new DefaultHttpClient(); HttpResponse response; try { response = httpclient.execute(httppost); } catch(Exception e){ e.printStackTrace(); } 
+1
source share

All Articles