我正在开发一个应用程序,我必须通过调用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 Service在服务器上上传多个base64图像。
答案 0 :(得分:1)
这是您通过multipart上传图片的代码。它将以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();
}