Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java HttpMethod.releaseConnection()和EntityUtils.consume(实体)存在问题_Java_Android_Rest_Apache Httpclient 4.x - Fatal编程技术网

Java HttpMethod.releaseConnection()和EntityUtils.consume(实体)存在问题

Java HttpMethod.releaseConnection()和EntityUtils.consume(实体)存在问题,java,android,rest,apache-httpclient-4.x,Java,Android,Rest,Apache Httpclient 4.x,我遇到了一个问题:我刚刚将以下代码从NetBeans复制到Eclipse(一个ADT项目)。我已经导入了在NetBeans中使用的所有库,但有两个错误,如下所示: EntityUtils.consume(实体)-类型EntityUtils的方法consume(HttpEntity)未定义 httpPut.releaseConnection()-HttpPut类型的方法releaseConnection()未定义 完整代码: import java.util.ArrayList; import j

我遇到了一个问题:我刚刚将以下代码从NetBeans复制到Eclipse(一个ADT项目)。我已经导入了在NetBeans中使用的所有库,但有两个错误,如下所示:

EntityUtils.consume(实体)-类型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;
        } 
import java.util.ArrayList;
导入java.util.List;
导入org.apache.http.HttpEntity;
导入org.apache.http.HttpResponse;
导入org.apache.http.NameValuePair;
导入org.apache.http.auth.AuthScope;
导入org.apache.http.auth.UsernamePasswordCredentials;
导入org.apache.http.client.entity.UrlEncodedFormEntity;
导入org.apache.http.client.methods.HttpPut;
导入org.apache.http.impl.client.DefaultHttpClient;
导入org.apache.http.message.BasicNameValuePair;
导入org.apache.http.params.HttpConnectionParams;
导入org.apache.http.util.EntityUtils;
导入com.android.lul.classes.User;
公共类用户服务{
私有静态最终字符串BASE_URI=”http://localhost:8080/LULServices/webresources";
公共静态字符串登录(字符串登录、字符串密码、字符串ipAdd)
{
字符串toReturn=null;
final DefaultHttpClient httpclient=新的DefaultHttpClient();
试一试{
httpclient.getCredentialsProvider().setCredentials(
新的AuthScope(“本地主机”,8080),
新用户名密码凭证(“xxxx”、“xxxx”);
HttpPut HttpPut=newhttpput(BASE_URI+“/services.users/login”);
HttpConnectionParams.setConnectionTimeout(httpclient.getParams(),10000);
addHeader(“内容类型”、“多部分/表单数据”);
List nameValuePairs=新的ArrayList();
添加(新的BasicNameValuePair(“登录”,“登录”));
添加(新的BasicNameValuePair(“密码”、“密码”));
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httpPut);
试一试{
HttpEntity=response.getEntity();
字符串putResponse=EntityUtils.toString(实体);
toReturn=反应;
EntityUtils.consume(实体);
}最后{
httpPut.releaseConnection();
}
}最后{
httpclient.getConnectionManager().shutdown();
回归回归;
} 
你能帮我吗


感谢检查您的导入;您在新项目中使用的Apache HttpComponents的版本是什么?

检查您的导入;您在新项目中使用的Apache HttpComponents的版本是什么?

Android附带了一个预打包的Apache HttpClient版本,该版本没有这些方法。它们不再支持DevelopmentMen而且那个代码已经过时了


Android团队建议您使用新代码,而不是HttpClient。更多信息可在Android开发者网站上找到。

Android附带的Apache HttpClient预打包版本没有这些方法。它们不再支持开发,而且代码已经过时


Android团队建议您使用新代码而不是HttpClient。更多信息可以在Android开发者网站上找到。

我认为您应该遵循一个更简单的示例,因为这个示例似乎是为了上传文件(多部分/表单数据),但关于代码中的具体问题:

  • 您可以执行
    entity.consumercontent();

  • HTTPRequest类(HttpPost,HttpPut)没有任何
    releaseConnection()
    方法。可能是因为它们不需要被释放(如果我错了,有人会纠正我)。该方法通常适用于持久性连接,但这里的情况并非如此


  • 我认为您应该遵循一个更简单的示例,因为这一示例似乎旨在上载文件(多部分/表单数据),但关于代码中的具体问题:

  • 您可以执行
    entity.consumercontent();

  • HTTPRequest类(HttpPost,HttpPut)没有任何
    releaseConnection()
    方法。可能是因为它们不需要被释放(如果我错了,有人会纠正我)。该方法通常适用于持久性连接,但这里的情况并非如此


  • 你理解错误的意思吗?是的,就像Robby说的,“没有这些方法的Apache HttpClient”。我认为这是一个导入问题,但我在NetBeans中使用的所有导入都很好。很好,那么你应该接受@Roby answerYes,我只是在检查Apache HttpClient是否有其他选项…只是为了不必更改我的所有代码。你明白错误的意思吗?是的,就像Robby说的,“没有这些方法的Apache HttpClient"。我认为这是一个导入问题,但我有我在NetBeans中使用的所有导入,并且都很好。很好,那么你应该接受@Roby answerYes,我只是在检查Apache HttpClient是否有其他选项…只是为了不必更改我的所有代码。该博客从2011年开始使用HttpUrlConnection加载文件是al最喜欢用汇编程序构建GUI。这就是android java开发的问题所在,他们正在尽最大努力让开发人员越来越难,从来都不喜欢android开发,讨厌它……2011年的博客使用HttpUrlConnection加载文件几乎就像用汇编程序构建GUI。这就是android java开发的问题所在对于开发者来说,他们正在尽最大努力让它变得越来越难,从来没有像android Develop那样