问题描述
限时送ChatGPT账号..我遇到了一个问题:我刚刚将以下代码从 NetBeans 复制到 Eclipse(一个 ADT 项目).我已经导入了在 NetBeans 中使用的所有相同的库,但我有 2 个错误,在以下几行中:
I'm having a problem: I've just copied the following code from NetBeans to Eclipse (an ADT project). I've imported all the same librarys I used in NetBeans, but I have 2 errors, in the following lines:
EntityUtils.consume(entity);
- 未定义 EntityUtils 类型的方法消耗(HttpEntity)
EntityUtils.consume(entity);
- The method consume(HttpEntity) is undefined for the type EntityUtils
httpPut.releaseConnection();
- 方法 releaseConnection() 未定义为 HttpPut 类型
httpPut.releaseConnection();
- The method releaseConnection() is undefined for the type HttpPut
完整代码:
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;
}
你能帮我吗?
谢谢
推荐答案
Android 附带了一个没有这些方法的预打包版本的 Apache HttpClient.他们不再支持开发,而且代码已经过时.
Android comes with a prepackaged version of Apache HttpClient that doesn't have those methods. They are no longer supporting development and that code is outdated.
Android 团队建议您使用 HttpUrlConnection 代替新代码HttpClient 的.更多信息可以在 Android 上的这个博客中找到开发者网站.
The Android team recommends that you use HttpUrlConnection for new code instead of HttpClient. More information can be found at this blog on the Android Developers site.
这篇关于HttpMethod.releaseConnection() 和 EntityUtils.consume(entity) 的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论