コード例 #1
0
ファイル: Provider.java プロジェクト: gilbertleung/MI-Chat
  @Override
  public Uri insert(Uri uri, ContentValues initialValues) {
    ContentValues values;
    if (initialValues != null) {
      values = new ContentValues(initialValues);
    } else {
      throw new SQLException("ContentValues argument for .insert() is null, cannot insert row");
    }

    SQLiteDatabase db = this.dbHelper.getWritableDatabase();

    // Validate the incoming uri
    db.beginTransaction();
    // Perform the update based on the incoming uri's pattern
    String insertTable = null;
    Uri baseUri = null;

    try {
      switch (uriMatcher.match(uri)) {
        case MATCHER_MESSAGES:
          insertTable = MessagesTable.TABLE_NAME;
          baseUri = MessagesTable.CONTENT_ID_URI_BASE;
          break;

        default:
          // Incoming URI pattern is invalid: halt & catch fire.
          throw new IllegalArgumentException("Unknown URI " + uri);
      }

      Log.d(TAG, values.keySet().toString());
      long newRowId = db.insert(insertTable, null, values);

      Log.d(TAG, Long.toString(newRowId));
      if (newRowId > 0) { // if rowID is -1, it means the insert failed
        // Build a new URI with the new resource's ID
        // appended to it.
        db.setTransactionSuccessful();
        Uri notifyUri = ContentUris.withAppendedId(baseUri, newRowId);
        // Notify observers that our data changed.
        getContext().getContentResolver().notifyChange(notifyUri, null);
        return notifyUri;
      }
    } finally {
      db.endTransaction();
    }

    /* insert failed; halt and catch fire */
    throw new SQLException("Failed to insert row into " + uri);
  }
コード例 #2
0
 public ConnectBuilder setBody(ContentValues cv) {
   try {
     String data = "";
     for (String key : cv.keySet()) {
       if (data.length() > 0) data += "&";
       data += URLEncoder.encode(key, "UTF-8");
       data += "=";
       data += URLEncoder.encode(cv.getAsString(key), "UTF-8");
     }
     Log.d(TAG, "BODY " + data);
     m_body = data.getBytes("UTF-8");
     m_contentType = "application/x-www-form-urlencoded; charset=utf-8";
   } catch (UnsupportedEncodingException e) {
     m_body = null;
   }
   return this;
 }
コード例 #3
0
 @Override
 public Set<String> keySet() {
   return contentValues.keySet();
 }