我遇到了一个问题:我刚刚将以下代码从 NetBeans 复制到 Eclipse(一个 ADT 项目)。我已经导入了我在 NetBeans 中使用的所有相同的库,但我有 2 个错误,在以下几行中:
EntityUtils.consume(entity);
- EntityUtils 类型的方法 consume(HttpEntity) 未定义
httpPut.releaseConnection();
- HttpPut 类型的方法 releaseConnection() 未定义
完整代码:
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.util.EntityUtils;
import com.android.lul.classes.User;
public class UserService {
private static final String BASE_URI = "http://localhost:8080/LULServices/webresources";
public static String Login (String login, String password, String ipAdd)
{
String toReturn = null;
final DefaultHttpClient httpclient = new DefaultHttpClient();
try {
httpclient.getCredentialsProvider().setCredentials(
new AuthScope("localhost", 8080),
new UsernamePasswordCredentials("xxxx", "xxxx"));
HttpPut httpPut = new HttpPut(BASE_URI + "/services.users/login");
HttpConnectionParams.setConnectionTimeout(httpclient.getParams(), 10000);
httpPut.addHeader("Content-type", "multipart/form-data");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("login", "login"));
nameValuePairs.add(new BasicNameValuePair("password", "password"));
httpPut.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httpPut);
try {
HttpEntity entity = response.getEntity();
String putResponse = EntityUtils.toString(entity);
toReturn = putResponse;
EntityUtils.consume(entity);
} finally {
httpPut.releaseConnection();
}
} finally {
httpclient.getConnectionManager().shutdown();
return toReturn;
}
你能帮助我吗?
谢谢