Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Have been in a somewhat similar situation during a pervasive positioning course.</p> <p>Here is what we did:</p> <p><strong>FileWriter</strong> The writing to file part was not an issue for us as we only collected GPS locations and not other sensor data. For us an implementation like below was sufficient. It is not clear if you have to write the data to the same file, if not, then you should be able to use it directly. </p> <pre><code>public class FileOutputWriter { private static String pathString = ""; private static String sensorString = ""; public static void setPath(Context context, String path) { pathString = path; File pathFile = new File(pathString); pathFile.mkdirs(); } public static void writeData(String data, String sensor, boolean append) { File file = new File(pathString + "/" + sensor+ ".log"); long timeStamp = System.currentTimeMillis(); BufferedWriter out = null; try { out = new BufferedWriter(new FileWriter(file, append)); out.write(timeStamp + ":" + data); out.newLine(); } catch (IOException e) { e.printStackTrace(); } finally { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } } </code></pre> <p><strong>Uploader</strong> To upload data to the server we create a cue of LogEntries (change this to your own dataholder object or simply String).</p> <pre><code>public class DataLogger { static Vector&lt;LogEntry&gt; log = new Vector&lt;LogEntry&gt;(); public static void addEnty(LogEntry entry) { Log.d("DEBUG", entry.getStrategy() + " added position to logger " + entry.getLocation()); log.add(entry); } public static Vector&lt;LogEntry&gt; getLog() { return log; } public static void clear() { log.clear(); } } </code></pre> <p>Notice that Vector is thread-safe.</p> <p>Finally we implemented a UploaderThread, responsible for periodically inspecting the DataLogger cue and upload added entries.</p> <pre><code>public class UploaderThread extends Thread { public static LinkedList&lt;String&gt; serverLog = new LinkedList&lt;String&gt;(); Boolean stop = false; Context c; public UploaderThread(Context c) { this.c = c; } public void pleaseStop() { stop = true; } @Override public void run() { while(!stop) { try { if(DataLogger.log.size() &gt; 0 &amp;&amp; Device.isOnline(c)) { while(DataLogger.log.size() &gt; 0) { LogEntry logEntry = DataLogger.getLog().get(0); String result = upload(logEntry); serverLog.add("("+DataLogger.log.size()+")"+"ServerResponse: "+result); if(result != null &amp;&amp; result.equals("OK")) { DataLogger.getLog().remove(0); } else { Thread.sleep(1000); } } } else { serverLog.add("Queue size = ("+DataLogger.log.size()+") + deviceIsonline: "+Device.isOnline(c)); } Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } } } private String upload(LogEntry entry) { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://yoururl/commit.php"); try { // Add your data List&lt;NameValuePair&gt; nameValuePairs = new ArrayList&lt;NameValuePair&gt;(); nameValuePairs.add(new BasicNameValuePair("tracename",sessionName +"-"+ entry.getStrategy().toString())); nameValuePairs.add(new BasicNameValuePair("latitude", Double.toString(entry.getLocation().getLatitude()))); nameValuePairs.add(new BasicNameValuePair("longitude", Double.toString(entry.getLocation().getLongitude()))); nameValuePairs.add(new BasicNameValuePair("timestamp", Long.toString(entry.getTimestamp()))); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); // Execute HTTP Post Request HttpResponse response = httpclient.execute(httppost); if(response != null) { InputStream in = response.getEntity().getContent(); String responseContent = inputStreamToString(in); return responseContent; } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } private String inputStreamToString(InputStream is) throws IOException { String line = ""; StringBuilder total = new StringBuilder(); // Wrap a BufferedReader around the InputStream BufferedReader rd = new BufferedReader(new InputStreamReader(is)); // Read response until the end while ((line = rd.readLine()) != null) { total.append(line); } // Return full string return total.toString(); } } </code></pre> <p>The thread is simply started in the first activity of your app:</p> <pre><code> UploaderThread ut; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); FileOutputWriter.setPath(this, Environment.getExternalStorageDirectory() .getAbsolutePath()); ut = new UploaderThread(this); ut.start(); } @Override protected void onDestroy() { super.onDestroy(); ut.pleaseStop(); } </code></pre> <p>Hope this gets you on the way</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload