【问题标题】:How to send multiple images in base64 format on server in android如何在android的服务器上以base64格式发送多个图像
【发布时间】:2013-11-06 20:04:13
【问题描述】:

我正在开发一个应用程序,我必须通过调用 Restful Web 服务在服务器上以 base64 格式发送多个图像。

调用Restful web服务的代码:

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();
        }

另一个问题是,当我使用 JSON 对象作为请求参数调用 Web 服务时,我得到了 线程池执行器。 我们如何解决这个问题。有没有通过调用 Restful Web Service 在服务器上上传多个 base64 图像的完美方法。

【问题讨论】:

  • 首先我认为JSONObject应该转换成Base64格式。之后,您可以将转换后的字符串发送到服务器。

标签: android


【解决方案1】:

这是您通过 mulitpart 上传图片的代码。它将以 base64 格式上传您的图像。

    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();
        }

【讨论】:

    【解决方案2】:

    我也遇到了同样的问题,但经过一些研究后我对其进行了排序。您不能通过restful webservice在json中发布多个图像。

    为此,您必须使用 xml 格式并点击服务。

    1)首先创建xml请求:

    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)现在您可以使用以下代码来访问 web 服务:

    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;
            }
    

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 2015-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多