gpt4 book ai didi

android - 无法在 Android 应用程序中发布 MultiPartEntity

转载 作者:行者123 更新时间:2023-11-29 21:42:24 25 4
gpt4 key购买 nike

我尝试在 Android 中使用 MultiPartEntity 和 HttpClient 发送图像,但不断收到异常:java.lang.UnsupportedOperationException: Multipart form entity does not implement #getContent()

这是我的代码:

public boolean enrollImage(String id, byte[] image) {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("https://example.com/"+id+"/images/");
httpPost.addHeader("Authorization", "Basic " + Base64.encodeToString(("user"+":"+"password").getBytes(),Base64.NO_WRAP));
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart("image", new ByteArrayBody(image, "image/jpg", "image.jpg"));
httpPost.setEntity(entity);
HttpResponse response;
try {
response = httpClient.execute(httpPost);
HttpEntity responseEntity = response.getEntity();
String responseString = EntityUtils.toString(entity);
JSONObject json = new JSONObject(responseString);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}

我添加了较新的 Apache 库 httpmime-4.2.5.jarhttpclient-4.2.5.jarhttpcore-4.2.4。 jarapache-mime4j-core-0.7.2.jar 到我的项目。

我怎样才能让它运行起来以便我可以发布

最佳答案

原来我只是混淆了我发送的请求实体和我想解析为 json 的响应实体。该代码的第 12 行应该是

String responseString = EntityUtils.toString(responseEntity);

String responseString = EntityUtils.toString(entity);

问题已解决。猜猜我该买rubber duck

关于android - 无法在 Android 应用程序中发布 MultiPartEntity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16742844/

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