Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package systems.ora.client;
  
  import java.net.URL;
  import java.util.List;
 import  org.json.*;
 
 public class OraClient {
 
   int    appId = 1000401;
   String baseURL = "http://sandbox.ora.me:6196/halo";
 
   public OraClient() {}
 
   private JSONObject jsonifyVertices(Halo h) {
     try {
       JSONObject jvrt = new JSONObject();
       jvrt.put("size"h.getSize());
       jvrt.put("color"h.getColor());
       jvrt.put("complexity"h.getComplexity());
       jvrt.put("speed"h.getSpeed());
       jvrt.put("brightness"h.getBrightness());
       jvrt.put("wobble"h.getWobble());
       return jvrt;
     } catch (JSONException ex) {
       return null;
     }
   }
 
   private String jsonifyHalo(Halo h) {
     try {
       JSONObject job = new JSONObject();
       job.put("app");
       if (h.getId() > 0) {
         job.put("id"h.getId());
       }
       job.put("vertices"jsonifyVertices(h));
       return job.toString();
     } catch (JSONException ex) {
       return null;
     }
   }
 
   private String jsonifyList(List<Halohalos) {
     try {
       JSONObject job = new JSONObject();
       job.put("app");
       JSONArray list = new JSONArray();
       for (Halo h : halos) {
         list.put(jsonifyVertices(h));
       }
       job.put("vertices"list);
       return job.toString();
     } catch (JSONException ex) {
       return null;
     }
   }
 
   private String readResponse(HttpURLConnection connthrows IOException {
     // Read it.
     BufferedReader in = new BufferedReader(
       new InputStreamReader(conn.getInputStream()));
     String inputLine;
     StringBuffer response = new StringBuffer();
     while((inputLine = in.readLine()) != null) {
       response.append(inputLine);
     }
     in.close();
     return response.toString();
   }
 
   private HttpURLConnection sendData(URL urlString methodString datathrows IOException {
 
 
     conn.setRequestMethod(method);
     conn.setRequestProperty("User-Agent""OraClient 0.1");
     conn.setRequestProperty("Content-Type""application/json");
     conn.setRequestProperty("Content-Length", Integer.toString(data.getBytes().length));
     conn.setUseCaches(false);
     conn.setDoInput(true);
     conn.setDoOutput(true);
 
     // Write body
     DataOutputStream out = new DataOutputStream(
       conn.getOutputStream());
     out.writeBytes(data);
     out.flush();
     out.close();
 
     return conn;
   }
 
   private Halo parseHalo(JSONObject jobthrows JSONException {
    JSONObject vert = job.getJSONObject("vertices");
    Halo h = new Halo(,
      vert.getDouble("size"),
      vert.getDouble("color"),
      vert.getDouble("complexity"),
      vert.getDouble("speed"),
      vert.getDouble("brightness"),
      vert.getDouble("wobble")
      );
    h.setId(job.getInt("id"));
    return h;
  }
  private Halo readHalo(HttpURLConnection connthrows IOException {
    String response = readResponse(conn);
    // Parse it.
    try {
      JSONObject job = new JSONObject(response);
      return parseHalo(job);
    } catch (JSONException ex) {
      return null;
    }
  }

  
Fetches the halo with specified id from the server. If the server is unavailable or otherwise reports an error, an exception will be thrown. If there is no such halo on the server, this returns null.

Parameters:
id The halo id
Returns:
The halo, if found. null if not.
Throws:
IOException On server error
  public Halo getHalo(int idthrows IOException {
    String route =  + "/" + Integer.toString(id);
    URL url = new URL(route);
    conn.setRequestMethod("GET");
    conn.setRequestProperty("User-Agent""OraClient 0.1");
    // Should we handle anything separate here?
    if (conn.getResponseCode() != 200) {
      return null;
    }
    Halo h = readHalo(conn);
    if (conn != null) {
      conn.disconnect();
    }
    return h;
  }

  
Sends a halo to the server.

Parameters:
halo A halo object to store.
Returns:
Updated halo.
Throws:
IOException On server failure.
  public Halo insertHalo(Halo halothrows IOException {
    URL url = new URL();
    String json = jsonifyHalo(halo);
    HttpURLConnection conn = sendData(url"POST"json);
    Halo h = readHalo(conn);
    return h;
  }

  
Persists the new state of a halo to the server.

Parameters:
halo A halo object to ubdate on the server.
Returns:
Updated halo.
Throws:
IOException On server failure.
  public Halo updateHalo(Halo halothrows IOException {
    URL url = new URL( + "/" + halo.getId());
    String json = jsonifyHalo(halo);
    HttpURLConnection conn = sendData(url"PUT"json);
    Halo h = readHalo(conn);
    return h;
  }
  
  
Sends a list of halos to the server.

Parameters:
halos A list of halos.
Returns:
the id for the collection of halos.
Throws:
IOException On server failure.
  public int insertHaloList(List<Halohalosthrows IOException {
    URL url = new URL( + "/collection");
    String json = jsonifyList(halos);
    HttpURLConnection conn = sendData(url"POST"json);
    String response = readResponse(conn);
    // Parse it.
    try {
      JSONObject job = new JSONObject(response);
      int collectionId = job.getInt("id");
      JSONArray jalos = job.getJSONArray("halos");
      for (int i = 0; i < jalos.length(); i++ ) {
        JSONObject jhob = jalos.getJSONObject(i);
        Halo h = halos.get(i);
        h.setId(jhob.getInt("id"));
      }
      return collectionId;
    } catch (JSONException ex) {
      return -1;
    }
  }
  
  
get a list of halos from the server.

Parameters:
collectionId The id of the collection.
Returns:
List of halos, if found.
Throws:
IOException On server failure.
  public ArrayList<HalogetHaloCollection(int collectionIdthrows IOException {
    URL url = new URL( + "/collection/" + Integer.toString(collectionId));
    conn.setRequestMethod("GET");
    conn.setRequestProperty("User-Agent""OraClient 0.1");
    // Should we handle anything separate here?
    if (conn.getResponseCode() != 200) {
      return null;
    }
    String response = readResponse(conn);
    ArrayList<Halohalos = new ArrayList<Halo>();
    try {
      JSONArray idList = new JSONArray(response);
      for (int i = 0; i < idList.length(); i++ ) {
        halos.add(getHalo(idList.getInt(i)));
      }
      return halos;
    } catch (JSONException ex) {
      return null;
    }
  }
New to GrepCode? Check out our FAQ X