0

I have a running async task in my android application which runs perfectly. However I would like to know if it possible to return a string value from the asynctask in android as well as update a UI item. I want to update to a rating stars in the UI update and return a list of information from my server (which is working now).

Is it possible to return a value and update a UI from one asynctask in android??

Code for my Asynctask is below:

public class ConnectToServer extends AsyncTask< String, String, ArrayList<String>> {
    @Override
    protected ArrayList<String> doInBackground(String... params) 
    {
        Socket client;
        DataInputStream input;
        DataOutputStream output;
        String results = "";
        XMLCreator x = new XMLCreator();
        ArrayList<String> lists = new ArrayList<String>();
        try 
        {
            Log.i("CLIENT", "Client started");

            // Step 1: Create a Socket to make connection.
            // client = new Socket(InetAddress.getLocalHost(), 500);
            client = new Socket("*****", 5001);
            Log.i("CLIENT", "Connected to: "
                    + client.getInetAddress().getHostName());
            publishProgress("Connected...");

            // Step 2: Get the input and output streams.
            input = new DataInputStream(client.getInputStream());
            output = new DataOutputStream(client.getOutputStream());
            Log.i("CLIENT", "Got I/O Streams");
            publishProgress("Obtained Streams...");

                // Step 3: Process connection.
            //Read in the value that the user selected from the bundle
            String Day = params[0].toString();

            //put into XML document as a query
            //create a string

            Document doc;
            try 
            {
                //Create an XML document with tutor's student number

                doc = x.createDoc();
                Element tutor = doc.createElement("Query");

                tutor.appendChild(x.createDayforSession(doc, "GetSessionByDay", Day));

                doc.appendChild(tutor);

                //Create a string
                String s = x.getStringFromDocument(doc);

                output.writeUTF(s);
            }
            catch (Exception e) 
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            publishProgress("Processing");



                //Get all the session information for that day in XML string
                //Create a document
                //Break it up and add it to the view
                results = input.readUTF();

                try 
                {
                    //Creates the document
                    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                    DocumentBuilder builder = factory.newDocumentBuilder();
                    Document document = builder.parse(new InputSource(new StringReader(results)));  

                    //optional, but recommended
                    //read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
                    document.getDocumentElement().normalize();

                        //Look at root node's type (e.g. <query> or <login> or <add>)
                        String rootNode = document.getDocumentElement().getNodeName().toString();

                        if (rootNode.equals("Result"))
                        {
                             //Any methods which need to be called that will be used to query the database
                            //Always sending the String XML through as a parameter
                            //Look at the child node

                            NodeList nList = document.getElementsByTagName("Query");

                            System.out.println("----------------------------");



                            for (int temp = 0; temp < nList.getLength(); temp++) 
                            {

                                Node nNode = nList.item(temp);

                                if (nNode.getNodeType() == Node.ELEMENT_NODE) 
                                {
                                    Element eElement = (Element) nNode;
                                    String Semester = eElement.getElementsByTagName("Semester").item(0).getTextContent();
                                    String StartTime = eElement.getElementsByTagName("StartTime").item(0).getTextContent();
                                    String Days = eElement.getElementsByTagName("Day").item(0).getTextContent();
                                    String Lab = eElement.getElementsByTagName("Lab").item(0).getTextContent();
                                    String session = "Semester: " + Semester + "\n" +  "StartTime: " + StartTime + "\n" + "Days: " + Days + "\n" + "Lab: " + Lab + "\n";
                                    lists.add(session);
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        e.getMessage();
                    }


            // Step 4: Close connection.
            Log.i("CLIENT", "Transmission complete. "
                    + "Closing connection.");
            output.writeUTF("TERMINATE");
            client.close();
            publishProgress("Closed connection...");

        } catch (IOException e) {
            e.printStackTrace();
        }
        return lists;

    }

    @Override
    protected void onPostExecute(ArrayList<String> lists) 
    {
        super.onPostExecute(lists);

        //UPDATE A STAR RATING HERE

    }

    @Override
    protected void onProgressUpdate(String... values) 
    {
        super.onProgressUpdate(values);
    }
}
user1397978
  • 255
  • 1
  • 7
  • 24

2 Answers2

0

You cannot return a value. But you can update the UI in onPostExecute(). Remove all super calls in your asynctask.

greenapps
  • 11,154
  • 2
  • 16
  • 19
0

you should override onPostExecute!! you can access your UI from this function and the inputs of this function is the result of doInBackground!!

strings95
  • 661
  • 11
  • 26