MqttConnectOptions成员未被Android Sudio 3.1.1识别

我正在写一个简单的mqtt客户端.由于某些奇怪的原因,Android studio 3.1.1无法解析MqttConnectOptions的任何成员函数.

编译器无法识别MqttConnectOptions的任何成员函数.

我在网上找到的所有示例都使用MqttConnectOptions成员.我很困惑!

我错过了什么简单的事情?

package com.grayraven.garage;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import org.eclipse.paho.android.service.MqttAndroidClient;
import org.eclipse.paho.client.mqttv3.IMqttActionListener;
import org.eclipse.paho.client.mqttv3.IMqttToken;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;



public class MainActivity extends AppCompatActivity {

    final String TAG = "Garage_Main";
    private static Context mContext = GarageApp.getAppContext();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this.getApplicationContext();
        setContentView(R.layout.activity_main);
    }

    final String broker = "10.211.1.127:1883";
    final String password = "monkey123";
    final String subscriptionTopic = "garage_door";


    MqttConnectOptions options = new MqttConnectOptions();
    options.setUserName("pi"); //cannot resolve setUserName!!!
    options.setPassword(password.tochararray()); //cannot resolve setPassword!!!!


    String clientId = MqttClient.generateClientId();
    MqttAndroidClient client = new MqttAndroidClient(mContext,broker,clientId);

   /// rest of code works so omitted for clarity

app build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "com.grayraven.garage"
        minSdkVersion 22
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'
        }
    }
}

repositories {
    maven {
        url "https://repo.eclipse.org/content/repositories/paho-releases/"
    }
}

dependencies {
    implementation filetree(dir: 'libs',include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    implementation('org.eclipse.paho:org.eclipse.paho.android.service:1.0.2') {
        exclude module: 'support-v4'
    }
}

解决方法

您导入Android的MQTT依赖项的代码不正确.请试试这个.
第1步:从build.gradle文件删除这些行

repositories {
    maven {
        url "https://repo.eclipse.org/content/repositories/paho-releases/"
    }
}

implementation('org.eclipse.paho:org.eclipse.paho.android.service:1.0.2') {
        exclude module: 'support-v4'
    }

第2步:将这些行添加到Android Studio项目的build.gradle文件中.这是顶级build.gradle文件,而不是app forder中您声明项目的所有依赖项的文件.

repositories {
    maven {
        url "https://repo.eclipse.org/content/repositories/paho-snapshots/"
    }
}

第3步:将这些行添加到Android应用的build.gradle文件

dependencies {
    compile 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.1.0'
    compile 'org.eclipse.paho:org.eclipse.paho.android.service:1.1.1'
}

最后清理,同步然后构建您的项目.

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...