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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:17:39+00:00 2026-05-23T11:17:39+00:00

I have a Service that launches an AsyncTask to retrive some data from a

  • 0

I have a Service that launches an AsyncTask to retrive some data from a device in my homenetwork. When the AsyncTask is done it passes the data read back to the Service using a callback-interface in onPostExecute that the Service implements. The service then reads the information in the passed String and does stuff depending on the information. This works great 9/10 times, but sometimes the String passed from the asynctask is null.

AsyncTask doInBackground:

private String result = "No results";
@Override
    protected Void doInBackground(Void... params) {
            try {
                    OutputStreamWriter wr = new OutputStreamWriter(mConnection.getOutputStream());
                    wr.write(body);
                    wr.flush();

                    BufferedReader rd = new BufferedReader(new InputStreamReader(mConnection.getInputStream()));
                    String line;
                    while ((line = rd.readLine()) != null) {
                            result = line;
                    }
                    wr.close();
                    rd.close();
            } catch (Exception e) {
                    result = e.getMessage();
            }
            return null;
    }

    protected void onPostExecute(Void result) {
            mListener.onHttpResponse(this.result, REQUEST_CODE);
    }

}

Service onHttpResponse:

@Override
    public void onHttpResponse(String response, int REQUEST_CODE) {
            ResponseEnterpreter mEnterpreter = new ResponseEnterpreter(response);
            switch (REQUEST_CODE) {
               case WidgetConstants.REQUEST_LED_STATE :
                   String artist = mEnterpreter.getArtist();  <--- NullPointerException 

ResponseEnterpreter getArtist:

public class ResponseEnterpreter {
    private String mResponse;


    public ResponseEnterpreter(String mResponse) {
            this.mResponse = mResponse;
    }
        public String getArtist() {
            int first_index = mResponse.lastIndexOf("&lt;dc:creator&gt;");
            if (first_index == -1) {
                    return null;
            }

            int last_index = mResponse.lastIndexOf("&lt;/dc:creator&gt;");
            String artist = mResponse.substring(first_index + "&lt;dc:creator&gt;".length(), last_index);

            return unEscapeString(artist);

    }

To my understanding, the result string is initialized as “No results” and if no result is retrived by the BufferedReader “No Result” is the String that should be passed along. The mConnection is set to 3sec timeout.

I’m really scratching my head over this….

Any ideas?

// Fredrik

  • 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-23T11:17:40+00:00Added an answer on May 23, 2026 at 11:17 am

    The onPostExecute methods gets the values returned from doInBackground as arguments so you should implement those methods differently:

    protected String doInBackground(Void... params) {
            try {
                    //[..]
    
            } catch (Exception e) {
                    return e.getMessage();
            }
            return result;
    }
    
    protected void onPostExecute(String result) {
            mListener.onHttpResponse(result, REQUEST_CODE);
    }
    

    You are overriding the result in every loop step, and you end only up with the last line of the document:

     while ((line = rd.readLine()) != null) {
         result = line;
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a service, that requests another class, which launches an AsyncTask Service->Weather Class->Execute
I have a wcf service that is hosted in IIS. This service launches some
I have a windows service (C#) installed on a server that launches every 10
I have an application that is launched from within a service using a local
I have a service that is downloading a file. When the download is done,
I have a Service downloading data every second from a web server (This Service
I'm trying to implement some code to import user's data from another service via
I have WCF service which launches the remote process from Process.Start successfully on stand
I have a service that extracting html code from an URL, converting it to
I have a relatively simple java service that fetches information from various SOAP webservices

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.