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 9100807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T00:57:32+00:00 2026-06-17T00:57:32+00:00

All, I have a small functioning app that makes an httprequest to a server

  • 0

All,

I have a small functioning app that makes an httprequest to a server based on user input sent by a buttonclick. I’m looking for some help to make my small app compatible with SDK 11+, by making this httprequest an async task.

I’ve spent some days reading about async tasks, and understand the principle, and the reasons for making the httprequest away from the UI thread.

However I just can’t get the code to work in my case. I can’t even get it to compile. I’ve included below my functioning code (ie before trying to make the httprequest asyncronous)

I’d be very grateful for some specific help. I apologise for my rubbish code and that variations of this question have already been answered.

Thanks in advance
Jamie

My MainActivity code is below:

package com.jrcdesign.ebookbeamer;
import java.io.IOException;
import org.apache.http.client.ClientProtocolException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.Spinner;
import android.view.View.OnClickListener;


public class MainActivity extends Activity {

Button sendButton;
Button btnSubmit;
EditText msgTextField;
EditText msg2TextField;
Spinner spinner1;

/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    // load the layout
    setContentView(R.layout.main);        

    // make message text field object
    msgTextField = (EditText) findViewById(R.id.msgTextField);
    msg2TextField = (EditText) findViewById(R.id.msg2TextField);

    // make send button object
    sendButton = (Button) findViewById(R.id.sendButton);
    btnSubmit = (Button) findViewById(R.id.btnSubmit);    
    addListenerOnButton();
    addListenerOnSpinnerItemSelection(); 

}            

    public void addListenerOnSpinnerItemSelection() {
    spinner1 = (Spinner) findViewById(R.id.spinner1);
    spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
    }

    // get the selected dropdown list value
    public void addListenerOnButton() {

    spinner1 = (Spinner) findViewById(R.id.spinner1);
    btnSubmit = (Button) findViewById(R.id.btnSubmit);

    btnSubmit.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View v) {

        Toast.makeText(MainActivity.this,
        "OnClickListener : " + 
                  "\nSpinner 1 : "+ String.valueOf(spinner1.getSelectedItem()),
            Toast.LENGTH_SHORT).show();

      msgTextField.setText("" + String.valueOf(spinner1.getSelectedItem()));

      }

    });
    }


    // Called when the SEND button is pressed
    // Need to make this an async task

    public void send(View v)
{
   // get the message from the message text box
    msgTextField.setText("" + String.valueOf(spinner1.getSelectedItem()));
    String msg = msgTextField.getText().toString();  
    String msg2 = msg2TextField.getText().toString();  

    if (msg.length()>0)
    {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://54.235.198.96/test1.php");
     try {
       List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
       nameValuePairs.add(new BasicNameValuePair("id", msg2));
       nameValuePairs.add(new BasicNameValuePair("message", msg));
       httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

       httpclient.execute(httppost);
       msgTextField.setText(""); // clear text box
       msg2TextField.setText(""); // clear text box

       Toast.makeText(MainActivity.this,
                "Your request is being processed",
                    Toast.LENGTH_LONG).show();

     } catch (ClientProtocolException e) {
         // TODO Auto-generated catch block
     } catch (IOException e) {
         // TODO Auto-generated catch block
     }

    }
    else
    {
        // display message if text fields are empty
        Toast.makeText(getBaseContext(),"All fields are required",Toast.LENGTH_SHORT).show();
    }

}

}
  • 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-17T00:57:34+00:00Added an answer on June 17, 2026 at 12:57 am

    Here is your Async Class:

    private class AsyncTaskDownloadSomething extends
            AsyncTask<String[], String, String> {
    
        DataClassLentABook  mData;
    
    
    
    
    
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            //Do some prepartations over here, before the task starts to execute
            //Like freeze the button and/or show a progress bar
    
    
        }
    
    
    
    
    
        @Override
        protected String doInBackground(String... urls) {
            // Task starts executing.
            String url = urls[0];
    
            // Execute HTTP requests here, with one url(urls[0]),
            // or many urls using the urls table
            // Save result in myresult
    
            return myresult;
    
        }
    
    
    
    
    
        protected void onPostExecute(String result) {
                   //Do modifications you want after everything is finished
                   //Like re-enable the button, and/or hide a progressbar
                   //And of course do what you want with your result got from http-req
    
    
    
        }
    }
    

    To execute your async task, when your button is clicked, just write this:

    new AsyncTaskDownloadSomething().execute(someURL);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small Silverlight app which downloads all of the images and text
I have a small code that doesn't want to work at all when I
I have docbook generated HTML that contains small, fixed size images. All these images
I have a small TCP server that listens on a port. While debugging it's
I have a small web app running on AppEngine and have all my URL
I have a small script that sets multiple cookies, they all have this format
I have small script that takes the value from a text input and needs
hello all I have a small dialog which I created dynamically, which has a
Possible Duplicate: USD Currency Formatting in Java All- I have a small problem in
I have a small frame on my site which has an 'overflow' attribute. All

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.