2014-06-30 3 views
0

완벽하게 실행되는 내 안드로이드 응용 프로그램에서 실행중인 비동기 작업이 있습니다. 그러나 UI 항목을 업데이트 할뿐만 아니라 안드로이드 asynctask에서 문자열 값을 반환 할 수 있는지 알고 싶습니다. UI 업데이트에서 등급 별로 업데이트하고 내 서버 (현재 작동 중)에서 정보 목록을 반환하고 싶습니다.asynctask android update ui 및 값을 반환

android에서 하나의 asynctask에서 값을 반환하고 UI를 업데이트 할 수 있습니까 ?? 내 AsyncTask를위한

코드 은 다음과 같습니다 :

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); 
    } 
} 

답변

0

당신은 값을 반환 할 수 없습니다. 하지만 onPostExecute()에서 UI를 업데이트 할 수 있습니다. asynctask에서 모든 수퍼 콜을 제거하십시오.

0

onPostExecute !!를 재정의해야합니다. 이 함수에서 UI에 액세스 할 수 있으며이 함수의 입력은 doInBackground의 결과입니다!