示例#1
0
  public void recognizeFromFileExplicit() throws Exception {
    File pcmFile = new File(testFileName);
    HttpURLConnection conn = (HttpURLConnection) new URL(serverURL).openConnection();

    // construct params
    JSONObject params = new JSONObject();
    params.put("format", "wav");
    params.put("rate", 16000);
    params.put("channel", "1");
    params.put("token", token);
    params.put("cuid", cuid);
    params.put("len", pcmFile.length());
    params.put("speech", DatatypeConverter.printBase64Binary(loadFile(pcmFile)));

    // add request header
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");

    conn.setDoInput(true);
    conn.setDoOutput(true);

    // send request
    DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
    wr.writeBytes(params.toString());
    wr.flush();
    wr.close();

    printResponse(conn);
  }
示例#2
0
  public void stateToStorage(State completeState, String fileName) {
    clearFile(fileName);

    JSONArray arrCompleted = new JSONArray();
    JSONArray arrIncompleted = new JSONArray();
    JSONArray arrUndetermined = new JSONArray();
    JSONArray arrReserved = new JSONArray();

    for (Event e : completeState.completedEvents) {
      JSONObject object = new JSONObject();
      object = castEventToJSONObj(e);
      arrCompleted.put(object);
    }
    for (Event e : completeState.incompletedEvents) {
      JSONObject object = new JSONObject();
      object = castEventToJSONObj(e);
      arrIncompleted.put(object);
    }
    for (ReservedEvent e : completeState.undeterminedEvents) {
      JSONObject object = new JSONObject();
      object = castFloatingEventToJSONObj(e);
      arrUndetermined.put(object);
    }
    for (ReservedEvent e : completeState.reservedEvents) {
      JSONObject object = new JSONObject();
      object = castReservedEventToJSONObj(e);
      arrReserved.put(object);
    }

    JSONObject o = new JSONObject();
    try {
      o.put("completed", arrCompleted);
      o.put("incompleted", arrIncompleted);
      o.put("undetermined", arrUndetermined);
      o.put("reserved", arrReserved);
    } catch (JSONException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    }

    try {
      FileWriter fw = new FileWriter(fileName, true);
      PrintWriter pw = new PrintWriter(fw);
      pw.println(o.toString());
      pw.close();
    } catch (IOException ex) {
      ex.printStackTrace();
    }
  }
  public void HttpPostJSON(String url, JSONObject js) throws IOException {
    String content = js.toString();

    URLConnection conn = new URL(url).openConnection();
    conn.setUseCaches(false);
    conn.setDoInput(true);
    conn.setDoOutput(true);
    conn.setRequestProperty("Content-Length", "" + content.length());
    conn.setRequestProperty("Content-Type", "application/json");

    OutputStream out = conn.getOutputStream();
    out.write(content.getBytes());
    out.close();

    BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String response = br.readLine();
    br.close();
    if (!response.equals(success))
      throw new IOException("Error when posting update to .NET service");
  }
 public void submit_order(String type, String amount, String price) {
   super.submit_order(type, amount, price);
   HashMap<String, String> args = new HashMap<String, String>();
   args.put("pair", "btc_usd");
   if (type.equals("bid")) {
     args.put("type", "buy");
   } else if (type.equals("ask")) {
     args.put("type", "sell");
   }
   args.put("rate", price);
   args.put("amount", amount);
   JSONObject reponse;
   try {
     reponse = new JSONObject(query("Trade", args));
     if (reponse.has("success") && reponse.getInt("success") == 1) {
       settings.lasttrade.setOrderid(
           Integer.toString(reponse.getJSONObject("return").getInt("order_id")));
     }
   } catch (JSONException e) {
     reponse = new JSONObject();
   }
   utils.logger.log(false, reponse.toString());
   needsToUpdateBlances = true;
 }