You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Android Studio中使用Java而非Kotlin调用Supabase服务?

当然可以在Android Studio中使用Java对接Supabase服务,官方提供了完整的Java兼容支持,以下是具体操作步骤:

1. 添加依赖

首先在Module级别的build.gradle文件中添加Supabase相关依赖(确保项目已配置mavenCentral仓库):

dependencies {
    // Supabase Android核心库
    implementation 'io.supabase:supabase-android:2.0.0'
    // 根据业务需求添加具体服务依赖,比如认证、存储、数据库等
    implementation 'io.supabase:supabase-auth:2.0.0'
    implementation 'io.supabase:supabase-storage:2.0.0'
    implementation 'io.supabase:supabase-database:2.0.0'
}
2. 初始化Supabase客户端

创建自定义Application类来全局初始化Supabase客户端,避免重复创建实例:

import io.supabase.SupabaseClient;
import io.supabase.SupabaseClientBuilder;

public class MyApplication extends android.app.Application {
    private static SupabaseClient supabaseClient;

    @Override
    public void onCreate() {
        super.onCreate();
        // 替换为你的Supabase项目URL和匿名公钥
        supabaseClient = new SupabaseClientBuilder()
                .setUrl("https://your-project-id.supabase.co")
                .setKey("your-anon-public-key")
                // 启用需要的服务
                .addAuth()
                .addStorage()
                .addDatabase()
                .build();
    }

    public static SupabaseClient getSupabaseClient() {
        return supabaseClient;
    }
}

然后在AndroidManifest.xml中注册该Application并添加网络权限:

<uses-permission android:name="android.permission.INTERNET" />

<application
    android:name=".MyApplication"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.MyApp">
    <!-- 你的Activity声明 -->
</application>
3. 使用Supabase服务示例

示例1:邮箱密码登录(认证服务)

import io.supabase.auth.Auth;
import io.supabase.auth.AuthUser;
import io.supabase.auth.exceptions.AuthException;

// 获取Auth服务实例
Auth auth = MyApplication.getSupabaseClient().getAuth();

// 网络请求必须在非主线程执行,避免ANR
new Thread(() -> {
    try {
        AuthUser user = auth.signInWithPassword("user@example.com", "your-password");
        // 登录成功,切换到主线程更新UI
        runOnUiThread(() -> {
            Toast.makeText(MainActivity.this, "登录成功:" + user.getEmail(), Toast.LENGTH_SHORT).show();
        });
    } catch (AuthException e) {
        // 登录失败,处理错误
        runOnUiThread(() -> {
            Toast.makeText(MainActivity.this, "登录失败:" + e.getMessage(), Toast.LENGTH_SHORT).show();
        });
    }
}).start();

示例2:数据库查询

import io.supabase.database.Database;
import io.supabase.database.exceptions.DatabaseException;

Database database = MyApplication.getSupabaseClient().getDatabase();

new Thread(() -> {
    try {
        // 查询"users"表的所有数据
        String queryResult = database.from("users").select("*").execute();
        // 解析JSON结果并在主线程更新UI
        runOnUiThread(() -> {
            // 处理查询结果,比如用Gson解析成实体类
        });
    } catch (DatabaseException e) {
        runOnUiThread(() -> {
            Toast.makeText(MainActivity.this, "查询失败:" + e.getMessage(), Toast.LENGTH_SHORT).show();
        });
    }
}).start();
4. 注意事项
  • 确保Supabase项目配置正确:比如允许匿名访问(若使用匿名公钥)、配置好表的访问规则。
  • 所有Supabase网络操作必须在非主线程执行,否则会触发ANR。
  • 务必处理异常,包括网络错误、认证失败、数据库权限不足等场景。
  • 可根据需求添加其他服务依赖,比如实时数据库implementation 'io.supabase:supabase-realtime:2.0.0'。

内容的提问来源于stack exchange,提问作者Shresth Mittal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 05:03:34