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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:47:07+00:00 2026-06-02T07:47:07+00:00

I have a method that I’m calling but, for newer versions of Android it

  • 0

I have a method that I’m calling but, for newer versions of Android it fails. Apparently, this is due to the lack of threading. My method is to send a message to my server. This is the code (sans threading)

public String sendMessage(String username, Editable message){
        BufferedReader in = null;
        String data = null;

        try{
            DefaultHttpClient client = new DefaultHttpClient();
            URI website = new URI("http://abc.com/user_send.php?username="+username+"&message="+message);

            HttpPost post_request = new HttpPost();
            post_request.setURI(website);


            HttpGet request = new HttpGet();

            request.setURI(website);
            //executing actual request
            HttpResponse response = client.execute(request);

            in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            StringBuffer sb = new StringBuffer("");
            String l = "";
            String nl = System.getProperty("line.separator");
            while ((l = in.readLine()) != null) {
                sb.append(l);

            }
            in.close();
            data = sb.toString();
            return data;
        }catch (Exception e){
            return "ERROR";
        }
    }  

Now, just trying to put a thread around it:

    public String sendMessage(String username, Editable message){
        BufferedReader in = null;
        String data = null;
Thread sendThread = new Thread(){
        try{
            DefaultHttpClient client = new DefaultHttpClient();
            URI website = new URI("http://thenjtechguy.com/njit/gds/user_send.php?username="+username+"&message="+message);

            HttpPost post_request = new HttpPost();
            post_request.setURI(website);


            HttpGet request = new HttpGet();

            request.setURI(website);
            //executing actual request
            HttpResponse response = client.execute(request);

            in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            StringBuffer sb = new StringBuffer("");
            String l = "";
            String nl = System.getProperty("line.separator");
            while ((l = in.readLine()) != null) {
                sb.append(l);

            }
            in.close();
            data = sb.toString();
            return data;
        }catch (Exception e){
            return "ERROR";
        }
} sendThread.start();   
} 

But that doesn’t work. What am I doing wrong? Also, if you notice I’m breaking any fundamental rules in android regarding the HttpClient, please let me know.

  • 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-02T07:47:09+00:00Added an answer on June 2, 2026 at 7:47 am

    Better to a head and use AsyncTask concept.

    AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.
    Refer this LINK for sample implementation

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

Sidebar

Related Questions

I have this method that downloads .csv files from yahoo finance and saves them
I have a method that receives an array and then stores this in the
I have a method that takes a callback argument to execute asynchronously, but the
I have a method that returns byte[] but for now would like to avoid
I have method that accept argument of Object type. But inside of method checked
We have method that looks like this : public String getCommandString(Object...args) { return this.code
I have a method that starts my game (startGame). I call it in viewDidLoad
I have a method that turns a byte array into an integer public int
I have class method that returns a list of employees that I can iterate
I have a method that where I want to redirect the user back to

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.