1

我正在开发一个应用程序,我必须通过调用 Restful Web Service 在服务器上以 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 服务时,我得到了 ThreadPoolExecutor。我们如何解决这个问题。是否有通过调用 Restful Web 服务在服务器上上传多个 base64 图像的完美方法。

4

2 回答 2

1

我也面临同样的问题,但经过一些研究后我对其进行了排序。你不能通过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)现在你可以使用下面的代码来访问webservice:

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

我希望它会帮助你。

于 2013-11-12T05:42:55.260 回答
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();
        }
于 2013-11-08T12:37:07.173 回答