コード例 #1
0
  // #{key}=#{URI.encode(val.to_s)}"
  // so could have add button in itemlist view which takes us to a form
  // should infer language settings from the list we are in
  // want to support addition of sound/image ...
  public CreateItemResult createItem(
      String cue,
      String cue_language,
      String character_cue_text,
      String part_of_speech,
      String response,
      String response_language,
      String character_response_text,
      String list_id) {
    String http_response = "";
    int status_code = 0;
    AndroidHttpClient client = null;
    try {

      URI uri = new URI("http://api.smart.fm/items");
      Log.d("DEBUG", uri.toString());
      HttpPost post = new HttpPost(uri);
      // Main.consumer.setTokenWithSecret(Main.ACCESS_TOKEN,
      // Main.TOKEN_SECRET);// TODO store in preferences ...
      // Main.consumer.sign(post);
      // set POST body
      String post_body =
          "cue[text]="
              + URLEncoder.encode(cue, "UTF-8")
              + "&cue[part_of_speech]="
              + part_of_speech
              + "&cue[language]="
              + cue_language
              + "&response[text]="
              + URLEncoder.encode(response, "UTF-8")
              + "&response[language]="
              + response_language
              + "&api_key="
              + Main.API_KEY
              + "&list_id="
              + list_id;
      if (character_response_text != null && !character_response_text.equals("")) {
        post_body +=
            "&character_response[text]=" + URLEncoder.encode(character_response_text, "UTF-8");
      }

      if (character_cue_text != null && !character_cue_text.equals("")) {
        post_body += "&cue[character]=" + URLEncoder.encode(character_cue_text, "UTF-8");
      }

      Log.d("DEBUG", post_body);
      // username/password here has to match the API key?
      String auth = Main.username(this) + ":" + Main.password(this);
      byte[] bytes = auth.getBytes();
      post.setHeader("Authorization", "Basic " + new String(Base64.encodeBase64(bytes)));
      // post.setHeader("Authorization",
      // "Basic dGFuc2FrdTpzYW1qb3NlcGg=");
      // [B@434f6610
      // setting content-type is overwritten ...
      post.setHeader("Content-Type", "application/x-www-form-urlencoded");

      post.setHeader("Host", "api.smart.fm");
      HttpEntity entity = new StringEntity(post_body, "UTF-8");
      post.setEntity(entity);

      Header[] array = post.getAllHeaders();
      for (Header h : array) {
        Log.d("DEBUG", h.toString());
      }
      client = AndroidHttpClient.newInstance("Main");
      HttpResponse response1 = client.execute(post);
      status_code = response1.getStatusLine().getStatusCode();
      Log.d("DEBUG", response1.getStatusLine().toString());
      array = response1.getAllHeaders();
      for (Header h : array) {
        Log.d("DEBUG", h.toString());
      }
      long length = response1.getEntity().getContentLength();
      byte[] response_bytes = new byte[(int) length];
      response1.getEntity().getContent().read(response_bytes);
      Log.d("DEBUG", new String(response_bytes));
      http_response = new String(response_bytes);
      // HttpEntity entity = response1.getEntity();
    } catch (IOException e) {
      /* Reset to Default image on any error. */
      e.printStackTrace();
    } catch (URISyntaxException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } finally {

      if (client != null) {
        client.close();
      }
    }

    return new CreateItemResult(status_code, http_response, list_id);
  }
コード例 #2
0
  // so there is question of checking for existing items (auto-completion?)
  // and uploading sounds and images ...
  public void onClick(View v) {
    EditText cueInput = (EditText) findViewById(R.id.cue);
    EditText responseInput = (EditText) findViewById(R.id.response);
    Spinner posInput = (Spinner) findViewById(R.id.pos);
    EditText characterResponseInput = (EditText) findViewById(R.id.response_character);
    EditText characterCueInput = (EditText) findViewById(R.id.cue_character);
    final String cue = cueInput.getText().toString();
    final String response = responseInput.getText().toString();
    final String pos = posInput.getSelectedItem().toString();
    final String character_cue = characterCueInput.getText().toString();
    final String character_response = characterResponseInput.getText().toString();
    String pos_code = Utils.POS_MAP.get(pos);
    if (TextUtils.isEmpty(pos_code)) {
      pos_code = "NONE";
    }
    final String final_pos_code = pos_code;

    if (Main.isNotLoggedIn(this)) {
      Intent intent = new Intent(Intent.ACTION_VIEW);
      intent.setClassName(this, LoginActivity.class.getName());
      intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); // avoid
      // navigation
      // back to this?
      LoginActivity.return_to = CreateItemActivity.class.getName();
      LoginActivity.params = new HashMap<String, String>();
      LoginActivity.params.put("list_id", list_id);
      LoginActivity.params.put("cue", cue);
      LoginActivity.params.put("response", response);
      LoginActivity.params.put("cue_language", cue_language);
      LoginActivity.params.put("response_language", response_language);
      LoginActivity.params.put("pos", pos);
      LoginActivity.params.put("character_cue", character_cue);
      LoginActivity.params.put("character_response", character_response);
      startActivity(intent);
    } else {
      // TODO cue and response languages need to be inferred from list we are
      // adding to ... Might want to fix those, i.e. not allow variation
      // on
      // search ...
      // TODO wondering whether there is some way to edit existing items
      // ...

      final ProgressDialog myOtherProgressDialog = new ProgressDialog(this);
      myOtherProgressDialog.setTitle("Please Wait ...");
      myOtherProgressDialog.setMessage("Creating Item ...");
      myOtherProgressDialog.setIndeterminate(true);
      myOtherProgressDialog.setCancelable(true);

      final Thread create_item =
          new Thread() {
            public void run() {
              // TODO make this interruptable .../*if
              // (!this.isInterrupted())*/
              CreateItemActivity.create_item_result =
                  createItem(
                      cue,
                      cue_language,
                      character_cue,
                      final_pos_code,
                      response,
                      response_language,
                      character_response,
                      list_id);

              myOtherProgressDialog.dismiss();
            }
          };
      myOtherProgressDialog.setButton(
          "Cancel",
          new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
              create_item.interrupt();
            }
          });
      OnCancelListener ocl =
          new OnCancelListener() {
            public void onCancel(DialogInterface arg0) {
              create_item.interrupt();
            }
          };
      myOtherProgressDialog.setOnCancelListener(ocl);
      myOtherProgressDialog.show();
      create_item.start();
    }
  }