gpt4 book ai didi

android - 如何在android中的服务器上以base64格式发送多张图片

转载 作者:行者123 更新时间:2023-11-29 21:30:43 34 4
gpt4 key购买 nike

我正在开发一个应用程序,我必须在其中通过调用 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图像。

最佳答案

这是您通过多部分上传图像的代码。它将以 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();
}

关于android - 如何在android中的服务器上以base64格式发送多张图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19628205/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com