学习Android,首先要学会如何配置环境,下面我以bmob后端云为例:

配置环境前的准备:

1.Android Studio(这里我用的是Android Studio3.5.1)

android tbs下载不下来_android

2.注册好的Bmob官方网址(https://www.bmob.cn/)

3.在Bmob上创建应用

配置过程:

1.将Gradle Scripts文件展开并打开第一项build.gradle文件

android tbs下载不下来_maven_02

2.把打开的build.gradle文件中的google()替换成

maven {
    url 'https://maven.google.com/'
    name 'Google'
}

在allprojects里面加上Bmob的maven仓库(maven { url "https://raw.github.com/bmob/bmob-android-sdk/master" })

,完整代码如下:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
        maven {
            url 'https://maven.google.com/'
            name 'Google'
        }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.5.1'
        
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
        maven {
            url 'https://maven.google.com/'
            name 'Google'
        }
        //Bmob的maven仓库地址--必填
        maven { url "https://raw.github.com/bmob/bmob-android-sdk/master" }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

3.把Module的build.gradle文件打开(Gradle Scripts目录下的第二项),先点击一下编译,若出现编译出错请尝试再次编译

android tbs下载不下来_android_03

编译成功后,添加Bmob官方文档提供依赖文件

implementation 'cn.bmob.android:bmob-sdk:3.7.3-rc1'
implementation "io.reactivex.rxjava2:rxjava:2.2.2"
implementation 'io.reactivex.rxjava2:rxandroid:2.1.0'
implementation 'com.squareup.okio:okio:2.1.0'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.squareup.okhttp3:okhttp:3.12.0'

在android里面添加useLibrary 'org.apache.http.legacy'

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "自己的包名"
        minSdkVersion 15
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    useLibrary 'org.apache.http.legacy'
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

    implementation 'cn.bmob.android:bmob-sdk:3.7.3-rc1'
    implementation "io.reactivex.rxjava2:rxjava:2.2.2"
    implementation 'io.reactivex.rxjava2:rxandroid:2.1.0'
    implementation 'com.squareup.okio:okio:2.1.0'
    implementation 'com.google.code.gson:gson:2.8.5'
    implementation 'com.squareup.okhttp3:okhttp:3.12.0'
}

添加后再次点击编译(Sync Now)

4.以上步骤完成后说明bmob-sdk已经加载完成,接下来在AndroidManifest.xml文件中添加相应的权限:

<!--从Bmob官方文档复制 -->
    <!--允许联网 -->
    <uses-permission android:name="android.permission.INTERNET" />
    <!--获取GSM(2g)、WCDMA(联通3g)等网络状态的信息  -->
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <!--获取wifi网络状态的信息 -->
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <!--保持CPU 运转,屏幕和键盘灯有可能是关闭的,用于文件上传和下载 -->
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <!--获取sd卡写的权限,用于文件上传和下载-->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <!--允许读取手机状态 用于创建BmobInstallation-->
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />

配置ContentProvider:

<application>
···
<provider
    android:name="cn.bmob.v3.util.BmobContentProvider"
    android:authorities="你的应用包名.BmobContentProvider">
</provider>
···
</application>

添加

android:usesCleartextTraffic="true"

 

 完整代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="自己的包名">

    <!--允许联网 -->
    <uses-permission android:name="android.permission.INTERNET" />
    <!--获取GSM(2g)、WCDMA(联通3g)等网络状态的信息  -->
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <!--获取wifi网络状态的信息 -->
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <!--保持CPU 运转,屏幕和键盘灯有可能是关闭的,用于文件上传和下载 -->
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <!--获取sd卡写的权限,用于文件上传和下载-->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <!--允许读取手机状态 用于创建BmobInstallation-->
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    
    <application
        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/AppTheme"
        android:usesCleartextTraffic="true">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <provider
            android:name="cn.bmob.v3.util.BmobContentProvider"
            android:authorities="自己的包名.BmobContentProvider">
        </provider>
    </application>

</manifest>

5.初始化BmobSDK

Bmob.initialize(this, "Your Application ID");

在Bmob官网上找到控制台进入(已注册),点击创建应用

android tbs下载不下来_android tbs下载不下来_04

创建成功后找到设置

android tbs下载不下来_maven_05

把Application ID复制下来粘贴到初始化的方法中如图

android tbs下载不下来_android_06

6.创建Person类继承BmobObject,参考Bmob官方文档

android tbs下载不下来_Android学习_07

Person类代码:

public class Person extends BmobObject {
    private String name;
    private String address;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
}

 

 7.测试数据添加

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Bmob.initialize(this, "自己的应用ID");

        Person p = new Person();
        p.setName("KANG");
        p.setAddress("北京海淀");
        p.save(new SaveListener<String>() {
            @Override
            public void done(String objectId, BmobException e) {
                if(e==null){
                    toast("添加数据成功,返回objectId为:"+objectId);
                }else{
                    toast("创建数据失败:" + e.getMessage());
                }
            }
        });
    }
    public void toast(String Message){
        Toast.makeText(this, Message, Toast.LENGTH_SHORT).show();
    }
}

运行成功并在显示添加成功信息 ,Bmon云数据库就会创建一个Person表和一条数据

android tbs下载不下来_ide_08