2016-09-21 45 views
-1

该应用程序的基本功能是从FTP服务器中存在的.csv文件获取实时数据(注意:服务器上的.csv文件会同时从另一个源同时更新),并在图形上显示这些值。最新值似乎并未得到更新。只有当我关闭并重新打开应用程序时,我才能看到显示的最新值。

我能够成功连接到FTP服务器并解析.csv文件并将所有记录的值显示在图形上。但是,最新的值似乎并没有在图上得到更新。只有当我关闭并重新打开应用程序时,才能看到图形上显示的最新值。

我试过下面的代码没有成功:

/* 
* Connects to a remote FTP server 
*/ 
void connectToFTP() { 

    try { 

     mFTPClient = new FTPClient(); 
     mFTPClient.connect(FTP_HOSTNAME, FTP_PORT); 
     returnCode = mFTPClient.getReplyCode(); 
     if (!FTPReply.isPositiveCompletion(returnCode)) { 
      throw new IOException("Could not connect"); 
     } 
     boolean loggedIn = mFTPClient.login(FTP_USER, FTP_PASSWORD); 
     if (!loggedIn) { 
      throw new IOException("Could not login"); 
     } 
     System.out.println("Connected and Logged in"); 
     statusUpdate.setText("Connected to FTP Server"); 
     mFTPClient.setFileType(FTP.BINARY_FILE_TYPE); 
     mFTPClient.enterLocalPassiveMode(); 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

void beginListenForData() throws IOException { 
    final Handler handler = new Handler(); 
    final byte delimiter = 10; //This is the ASCII code for a newline character 


    stopWorker = false; 
    readBufferPosition = 0; 
    readBuffer = new byte[1024]; 
    mmInputStream = mFTPClient.retrieveFileStream(fileName); 

    workerThread = new Thread(new Runnable() { 
     public void run() { 
      while (!Thread.currentThread().isInterrupted() && !stopWorker) { 
       try { 
        Log.i("Inside begindata ", "data " + count++); 
        int bytesAvailable = mmInputStream.available(); 
        if (bytesAvailable > 0) { 
         byte[] packetBytes = new byte[bytesAvailable]; 
         mmInputStream.read(packetBytes); 
         for (int i = 0; i < bytesAvailable; i++) { 
          byte b = packetBytes[i]; 
          if (b == delimiter) { 
           byte[] encodedBytes = new byte[readBufferPosition]; 
           System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length); 
           final String data = new String(encodedBytes, "US-ASCII"); 
           readBufferPosition = 0; 

           handler.post(new Runnable() { 
            public void run() { 
             myLabel.setText(data); 

             String s = data; 
             String[] values = data.split(","); 
             Log.i("Text ", "data " + values[3]); 
             datalist.add(values[3]); 
             power.setText(values[3]); 



            } 
           }); 
          } else { 
           readBuffer[readBufferPosition++] = b; 
          } 
         } 
         // dataPoints(); 
        } 
       } catch (IOException ex) { 
        stopWorker = true; 
       } 
      } 
     } 
    }); 

    workerThread.start(); 
} 
+1

将您的解析值存储在SQLite数据库中,并使用内容提供者刷新数据。 –

+0

我是新来的android和SQLite。你能举个例子吗? –

回答

0

使用异步任务从服务器下载数据并更新onPostExecute方法的UI。

从Activity的“onCreate”方法启动此异步任务。

+0

我是android新手。你能举个例子吗? –

相关问题