gpt4 book ai didi

java - 找不到类 'org.apache.http.entity.mime.content.Filebody',从方法中引用

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:33:20 32 4
gpt4 key购买 nike

我想做的是将图片发送到网络服务器。当我在我的 Android 项目中调用方法时,出现以下错误:找不到类“org.apache.http.entity.mime.content.Filebody”,引用自方法 com.example.tc.Send.send。

即使我有以下导入,也会发生这种情况:

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.DefaultHttpClient;

这是方法所在的类的样子:

public class Send {
public Send(){
}

public static String send(String path) throws Exception {
String filePath = path;
String svar;

HttpClient httpclient = new DefaultHttpClient();
try {
HttpPost httppost = new HttpPost("path to web server");
FileBody pic = new FileBody(new File(filePath));
MultipartEntity requestEntity = new MultipartEntity();
requestEntity.addPart("file", pic);

httppost.setEntity(requestEntity);
System.out.println("executing request " + httppost.getRequestLine());
HttpResponse response = httpclient.execute(httppost);
HttpEntity responseEntity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());

ByteArrayOutputStream outstream = new ByteArrayOutputStream();
response.getEntity().writeTo(outstream);
byte [] responseBody = outstream.toByteArray();
svar = new String(responseBody);
System.out.println(svar);

} finally {
try {
httpclient.getConnectionManager().shutdown();
}
catch (Exception ignore) {
}
}
return svar;
}
}

谁能看出是什么问题?

最佳答案

添加这两个依赖

compile "org.apache.httpcomponents:httpcore:4.2.4"
compile "org.apache.httpcomponents:httpmime:4.3"

关于java - 找不到类 'org.apache.http.entity.mime.content.Filebody',从方法中引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16671939/

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