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

  • SEARCH
  • Home
  • 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 6910255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:47:28+00:00 2026-05-27T08:47:28+00:00

I recently made a question based on doing a HTTP request with POST data

  • 0

I recently made a question based on doing a HTTP request with POST data (Found here: Android API 14 – POST Data to HTTP) and I was told I would need to try something like an AsyncTask because I cannot carry out Network Operations in the main thread.

In short form, I have no idea how to do this. Any help is appreciated! Here is my code:

package me.babblebox.application;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
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.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;

public class BabbleBoxActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    public void check_login() {
        // Create a new HttpClient and Post Header
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://babblebox.me/android/test.php");

        try {
            // Add your data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("id", "12345"));
            nameValuePairs.add(new BasicNameValuePair("stringdata", "Hi"));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);

        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
        } catch (IOException e) {
            // TODO Auto-generated catch block
        }
    }
    public void check_login_button(View v) {
           check_login();
    }
}
  • 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-05-27T08:47:28+00:00Added an answer on May 27, 2026 at 8:47 am

    Read about Processes and threads also AsyncTask.

    The following is far from perfect but you could try something like this to get started…

    public class BabbleBoxActivity extends Activity {
    
        // Leave onCreate() as it is
    
        public void check_login_button(View v) {
            PostTask postTask = new PostTask();
            postTask.execute();
        }
    
        // EDITED THE LINE BELOW TO INCLUDE 'class'
        private class PostTask extends AsyncTask<Void, Void, Void> {
    
            // Move the code that was in check_login to the
            // doInBackground method below
    
            protected Void doInBackground(Void... params) {
    
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://babblebox.me/android/test.php");
    
                try {
                    // Add your data
                    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                    nameValuePairs.add(new BasicNameValuePair("id", "12345"));
                    nameValuePairs.add(new BasicNameValuePair("stringdata", "Hi"));
                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    
                    // Execute HTTP Post Request
                    HttpResponse response = httpclient.execute(httppost);
    
                } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                }
                return null;
            }
        }
    }
    

    In reality you’ll need to learn how to pass paramaters into the AsyncTask and get it to return valid results.

    EDIT:

    Just out of interest, I put together your original code (without AsyncTask but on Android v2.2 which allows network operations on main thread). I added some logging code just to check the connection worked…

    HttpResponse response = httpclient.execute(httppost);
    Header[] headers = response.getAllHeaders();
    Log.d("BabbleBox", "Header count: " + headers.length);
    for (int c = 0; c < headers.length; c++)
        Log.d("Babblebox", "Header: name=" + headers[c].getName() + " value=" + headers[c].getValue());
    

    …and I got the following response headers.

    Header count: 6
    Header: name=Date value=Sun, 11 Dec 2011 16:38:19 GMT
    Header: name=Server value=LiteSpeed
    Header: name=Connection value=close
    Header: name=X-Powered-By value=PHP/5.3.8
    Header: name=Content-Type value=text/html
    Header: name=Content-Length value=4
    

    The response from any web ‘request’ (GET, POST, PUT) will vary depending on the remote service you are talking to. You’ll need to work out what your server is going to return and then process the response accordingly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question has some relation to this post I made recently: Drag a UIView
Recently posted a question regarding the HttpClient over Https ( found here ). I've
I've recently made another question about connecting to MS-ACCESS database with .NET in C#
I just recently made the move to Objective-C. I am doing some exercises from
Please see here for a question I recently asked. The only answer you should
I have the following question, recently I started with the study of Android and
For reference, here is a question on SO that I asked recently that is
I am performing HTTP Basic Authentication in my Android application. Recently, however, I have
Question Is there a usable facebook entry point to the Data Storage API that
I recently made this wordpress blog, where you can sign up a team for

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.