我正在写一个简单的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文件中删除这些行
第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' }
最后清理,同步然后构建您的项目.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。