DataClient.java :  » Log » nimbitsopen » com » nimbits » Android Open Source

Android Open Source » Log » nimbitsopen 
nimbitsopen » com » nimbits » DataClient.java
package com.nimbits;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;

 

public class DataClient {
   
  private final static String host = "http://app.nimbits.com";
  
  
  public static ArrayList<Double> getCurrentValues( ArrayList<String> points) 
  {
    ArrayList<Double> retVal = new  ArrayList<Double>();
    //  
    //    String data = URLEncoder.encode("key", "UTF-8") + "=" + URLEncoder.encode(soaKey, "UTF-8");
    //     data += "&" + URLEncoder.encode("pointname", "UTF-8") + "=" + URLEncoder.encode(pointName, "UTF-8"); 

  //  Log.v("getCurrentValues","" + points.size());
    try {
      GoogleAuthentication G;
      G = GoogleAuthentication.getGoogleAuthentication();

      for (String s : points)
      {
       
        URL url = new URL(host + "/nimbits/Service/value?pointname=" +  URLEncoder.encode(s,"UTF-8"));
        URLConnection conn = url.openConnection ();
        conn.addRequestProperty("Cookie",G.getAuthCookie().getName() + "=" + G.getAuthCookie().getValue());

        conn.setDoOutput(true);
        BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line;
        String result ="";

        while ((line = rd.readLine()) != null) {
          result +=line;
        }
        rd.close();
        double d  = Double.valueOf(result);
        retVal.add(d);
      }
    } catch ( Exception e) {
       
    }





    return retVal;

  }
  public static void recordValue(String pointName,double value) throws IOException
  {

    GoogleAuthentication G;
    G = GoogleAuthentication.getGoogleAuthentication();
    URL url = new URL(host + "/nimbits/Service/value");
    URLConnection conn = url.openConnection ();

    conn.addRequestProperty("Cookie",G.getAuthCookie().getName() + "=" + G.getAuthCookie().getValue());
    conn.setDoOutput(true);
    OutputStreamWriter out = new OutputStreamWriter(
        conn.getOutputStream());
  //  Log.v("f", "pointname=" +  URLEncoder.encode(pointName,"UTF-8")  + "&timestamp=" + new Date().getTime() + "&value=" + value);
    out.write("pointname=" +  URLEncoder.encode(pointName,"UTF-8") + "&timestamp=" + new Date().getTime() + "&value=" + value);
    out.flush();
    out.close();
    // Get the response
    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String line;
    while ((line = rd.readLine()) != null) {
      //Log.v("Recordinging",line);
      // Process line...
      System.out.println(line);
    }

    rd.close();
  }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.