HttpMethod.releaseConnection() 和 EntityUtils.consume(entity) 的问题

编程入门 行业动态 更新时间:2024-10-27 11:24:56
本文介绍了HttpMethod.releaseConnection() 和 EntityUtils.consume(entity) 的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送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:关键词]

本文发布于:2023-04-19 14:30:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/963833.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:releaseConnection   HttpMethod   EntityUtils   entity   consume

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!