Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8937645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:26:59+00:00 2026-06-15T10:26:59+00:00

I am trying to input text from Android into websites, and I read that

  • 0

I am trying to input text from Android into websites, and I read that httppost is a good option. I download the HttpClient 4.2.2 (GA) tar.gz, unzipped them, and copied the 7 jars into the lib folder of my android project in Eclipse. I’m pretty sure I got all the jars, since they matched those listed on the website.

I then proceeded to copy and paste the top tutorial from: http://hc.apache.org/httpcomponents-client-ga/quickstart.html

I imported everything, and was left with this error:

   EntityUtils.consume(entity1);  //X
    } finally {
       httpGet.releaseConnection();  //X

This portion of code is at two places in the tutorial, and errors occur at both.
Eclipse says for the first line:

“The method consume(HttpEntity) is undefined for the type EntityUtils.”

Second line:

“The method releaseConnection() is undefined for the type HttpGet.”

I’m pretty sure I downloaded every jar, transported them correctly, and imported everything. What is making the error? Thanks.

Here is what I have now. Edward, I used some of the code from your methods, but just put them into onCreate. However, this isn’t working. A few seconds after I go from the previous activity to this one, I get the message that the app “has stopped unexpectedly”.

I have a question about inputting my Strings into the website text fields: Do I use NameValuePairs of HttpParams? Here’s my code, can you see what’s wrong? Thanks.

    package com.example.myapp;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.HttpClientParams;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.Toast;

public class BalanceCheckerActivity extends Activity {

private final String LOGIN_URL = "https://someloginsite.com"; //username and password

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_balance_checker);

        String username = getIntent().getExtras().getString("username");
        String password = getIntent().getExtras().getString("password");

        //Building post parameters, key and value pair
        List<NameValuePair> accountInfo = new ArrayList<NameValuePair>(2);
        accountInfo.add(new BasicNameValuePair("inputEnterpriseId", username));
        accountInfo.add(new BasicNameValuePair("password", password));

        //Creating HTTP client
        HttpClient httpClient = new DefaultHttpClient();

        //Creating HTTP Post
        HttpPost httpPost = new HttpPost(LOGIN_URL);

        BasicHttpParams params = new BasicHttpParams();
        params.setParameter("inputEnterpriseID", username);
        params.setParameter("password", password);
        httpPost.setParams(params);

        //Url Encoding the POST parameters
        try {
            httpPost.setEntity(new UrlEncodedFormEntity(accountInfo));
        }
        catch (UnsupportedEncodingException e) {
            // writing error to Log
            e.printStackTrace();
            startActivity(new Intent(this, AccountInputActivity.class));
        }

        HttpResponse response = null;
        InputStreamReader iSR = null;
        String source = null;

     // Making HTTP Request
        try {
            response = httpClient.execute(httpPost);

            // writing response to log
            Log.d("Http Response:", response.toString());

            iSR = new InputStreamReader(response.getEntity().getContent());

            BufferedReader br = new BufferedReader(iSR);   
            source = "";

            while((source = br.readLine()) != null)
            {
                source += br.readLine();
            }

        } catch (ClientProtocolException e) {
            // writing exception to log
            e.printStackTrace();
            startActivity(new Intent(this, AccountInputActivity.class));

        } catch (IOException e) {
            // writing exception to log
            e.printStackTrace();
            startActivity(new Intent(this, AccountInputActivity.class));
        }
        System.out.println(source);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_balance_checker, menu);
        return true;
    }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T10:26:59+00:00Added an answer on June 15, 2026 at 10:26 am

    That mostly looks pretty good to me. I only saw one obviously wrong piece of code in it:

    while((source = br.readLine()) != null)
    {
        source += br.readLine();
    }
    

    That’s kind of a mess, and rather than try to untangle it, I’ll just rewrite it.

    StringBuilder sb = new StringBuilder();
    String line;
    while ((line = br.readLine()) != null)
      sb.append(line);
    String source = sb.toString();
    

    Also, you shouldn’t be doing network I/O from onCreate() or even from within your UI thread, since it can block for a long time, freezing your entire UI and possibly causing an “Application Not Responding” (ANR) crash. But for a simple test program, you can let that slide for now. For production code, you’d launch a thread or use AsyncTask().

    Anyway, we’re not really interested in building and debugging your program for you. Have you tried this code out? What was the result?

    One final note: a login sequence like this is likely to return an authentication token in the form of a cookie. I forget how you extract cookies from an HttpResponse, but you’ll want to do that, and then include any received cookies as part of any subsequent requests to that web site.


    Original answer:

    I think you’ve gotten yourself all tangled up. The Apache http client package is built into Android, so there’s no need to download any jar files from apache.

    I’m not familiar with EntityUtils, but whatever it is, if you can avoid using it, I would do so. Try to stick with the bundled API whenever possible; every third-party or utility library you add to your application increases bloat, and on mobile devices, you want to keep your application as light as possible. As for the actual “consume()” method not being found, that’s probably a mistake in the documentation. They probably meant consumeContent().

    The releaseConnection() call is probably only necessary for persistent connection. That’s relatively advanced usage; I don’t even do persistent or managed connections in my own code.

    You haven’t provided enough information to let us know what it is you’re trying to do, but I’ll try give you a reasonably generic answer.

    There are many, many ways to transmit data to a server over the http protocol, but in the vast majority of cases you want to transmit form-encoded data via HttpPost.

    The procedure is:

    • Create a DefaultHttpClient
    • Create an HttpPost request
      • Add headers as needed with setHeader() or addHeader().
      • Add the data to be transmitted in the form of an HttpEntity
    • Call client.execute() with the post request
    • Wait for and receive an HttpResponse; examine it for status code.
    • If you’re expecting data back from the server, use response.getEntity()

    There are many HttpEntity classes, which collect their data and transmit it to the server each in their own way. Assuming you’re transmitting form-encoded data, then UrlEncodedFormEntity is the one you want. This entity takes a list of NameValuePair objects which it formats properly for form-encoded data and transmits it.

    Here is some code I’ve written to do this; these are only code fragments so I’ll leave it to you to incorporate them into your application and debug them yourself.

    /**
     * POST the given url, providing the given list of NameValuePairs
     * @param url   destination url
     * @param data  data, as a list of name/value pairs
     */
    public HttpResponse post(String url, List<NameValuePair> data) {
        HttpPost req = new HttpPost(url);
        UrlEncodedFormEntity e;
        try {
            e = new UrlEncodedFormEntity(data, "UTF-8");
        } catch (UnsupportedEncodingException e1) {
            Log.e(TAG, "Unknown exception: " + e1);
            return null;   // Or throw an exception, it's up to you
        }
        return post(req, e);
    }
    
    /**
     * Post an arbitrary entity.
     * @param req   HttpPost
     * @param data  Any HttpEntity subclass
     * @return HttpResponse from server
     */
    public HttpResponse post(HttpPost req, HttpEntity data) {
        try {
            HttpClient client = new DefaultHttpClient();
            req.setEntity(data);
            HttpResponse resp = client.execute(req);
            int status = resp.getStatusLine().getStatusCode();
            if (status != HttpStatus.SC_OK) {
                Log.w(TAG,
                  "http error: " + resp.getStatusLine().getReasonPhrase());
                return null;   // Or throw an exception, it's up to you
            }
            return resp;
        } catch (ClientProtocolException e) {
            Log.e(TAG, "Protocol exception: " + e);
            return null;
        } catch (UnknownHostException e) {
            return null;
        } catch (IOException e) {
            Log.e(TAG, "IO exception: " + e);
            return null;
        } catch (Exception e) {
            // Catch-all
            Log.e(TAG, "Unknown exception: " + e);
            return null;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get a user input from Edit Text into List View, I
I am trying to get text input from the keyboard in Java 6. I
I am trying to input data from a text file in C++. The text
I'm trying to assign a variable from a html text input value and then
I've been trying to use Robot from awt, to input some text on a
I am trying to create an SEO-friendly link from the input in a text
i have been trying to copy text input from alertview textfield to a NSMutableArray
I am trying to create a dynamic lambda expression (parsed from text), that does
I'm trying to get the text from a text box. I have 2 input
What I want my program to do is to read an input text from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.