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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:50:22+00:00 2026-06-01T06:50:22+00:00

I am relatively a new Android developer and I am not able to understand

  • 0

I am relatively a new Android developer and I am not able to understand how to do this. I have been looking through all the forums, I made some advance but still here I am.
So, what I want to do is a common function that send a POST request to a webpage (it only sends one POST argument) and returns the result as a string.
I have the main thread here

public class AppActivity extends Activity {
    HTTPPostData PostData = new HTTPPostData("id");
    PostData.execute();
    txtLabel.setText(PostData.Result);
}

and I have my HTTPPostData asynchronous class

public class HTTPPostData extends AsyncTask<String, Long, Object> {
String Value = null;
String Result = null;

public HTTPPostData(String query) {
    Value = query;
    }

@Override
protected String doInBackground(String... params) {
    byte[] Bresult = null;
        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost("http://www.mypage.com/script.php");
        try {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
        nameValuePairs.add(new BasicNameValuePair("cmd", Value));
            post.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
            HttpResponse response = client.execute(post);
            StatusLine statusLine = response.getStatusLine();
            if(statusLine.getStatusCode() == HttpURLConnection.HTTP_OK){
                Bresult = EntityUtils.toByteArray(response.getEntity());
                Result = new String(Bresult, "UTF-8");
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (Exception e) {
        }
        return Result;
    }
}

I want to use this function several times (inside the same Activity or share it with other Activities of the same application). I am a little bit messed up at this moment so I need your help. What I understand is that I am asking for the result before the doInBackground() is done, and I get an empty result.

Thanks in advance for your help

  • 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-01T06:50:23+00:00Added an answer on June 1, 2026 at 6:50 am

    Regarding this:

    HTTPPostData PostData = new HTTPPostData("id");
    PostData.execute();
    txtLabel.setText(PostData.Result);
    

    Your problem is that you’re treating asynctask like it’s just a regular function. It’s good that you move webpage loading off the main thread, but if you depend on the result for the very next instruction, then it’s not doing you much good, you’re still blocking the main program waiting for the result. You need to think of AsyncTask like a ‘fire and forget’ operation, in which you don’t know when, if ever, it will come back.

    The better thing to do here would be something like:

    HTTPPostData PostData = new HTTPPostData("id");
    PostData.execute();
    txtLabel.setText("Loading...");
    

    and then in the asynctask:

    protected void onPostExecute(String result) {
         txtLabel.setText(result);
    }
    

    This lets your main thread get on with doing it’s business without knowing the result of the asynctask, and then as soon as the data is available the asynctask will populate the text label with the result.

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

Sidebar

Related Questions

I am a relatively new Android developer but have gotten pretty familiar with the
I am relatively new to android. I have two applications A and B. I
I'm relatively new to java and android development so sorry if this is a
I'm relatively new to Java, eclipse and android so this could be a completely
I am relatively new to Android Animation and Gestures. I have 15 images that
I am relatively new to Android development but I do have a pretty good
I`m relatively new at Android development, although I have created a few basic applications.
Hey all, I'm still relatively new to Java, and looking for a sanity check.
I am relatively new to Android development. My requirement is such that I have
I'm relatively new to Android programming, and am having a problem with inflation. I'm

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.