Problems with HttpMethod.releaseConnection() and E

2019-08-19 06:55发布

问题:

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); - The method consume(HttpEntity) is undefined for the type EntityUtils

httpPut.releaseConnection(); - The method releaseConnection() is undefined for the type HttpPut

Complete code:

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;
        } 

Can you help me?

Thanks

回答1:

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.

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.



回答2:

I think you should follow a simpler example, as this one seems to be meant to upload files (multipart/form-data), but regarding the specific problems in your code:

  1. Instead of EntityUtils.consume(entity); you can do entity.consumeContent(); http://developer.android.com/reference/org/apache/http/HttpEntity.html

  2. The HTTPRequest classes (HttpPost, HttpPut) don't have any releaseConnection() method. Probably because they don't need to be released (somebody correct me if I'm wrong). That method is usually applicable for persistent connections which is not the case here.



回答3:

Check your imports; What version of Apache HttpComponents are you using in your new project?