我正在尝试在
Android上创建一个MqttClient,在ADT上使用Android模拟器(Nexus7)(在Linux Fedora17上使用Eclipse 3.8.0).
我发现这个问题回答了 (Android MQTT unable to create client),但它未能解决我的问题.
我发现这个问题回答了 (Android MQTT unable to create client),但它未能解决我的问题.
我创建了一个活动,允许用户进入存储presistence文件的目录,但是没有办法避免捕获异常.
我试过“/ mnt / sdcard /”,“/ mnt /”,“/ mnt / sdcard / tmp /”等.
在Eclipse项目中,我是否需要关注Android模拟器中的特定设置?
是否有任何使用权限包含在应用程序中?
我看了引用的回答问题中提到的各种mqtt资源,但我无法找到答案.
这是我的代码:
package com.storassa.android.mqttsample; import org.eclipse.paho.client.mqttv3.MqttClient; import org.eclipse.paho.client.mqttv3.MqttDefaultFilePersistence; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.AutoCompleteTextView; import android.widget.Button; import android.widget.TextView; public class MqttSample extends Activity { Button okButton = null; AutoCompleteTextView inputTextView = null; TextView errorTextView = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_mqtt_sample); okButton = (Button)findViewById(R.id.okButton); inputTextView = (AutoCompleteTextView)findViewById(R.id.InputText); errorTextView = (TextView)findViewById(R.id.errorText); okButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub try { String dir = inputTextView.getText().toString(); MqttDefaultFilePersistence mdfp = new MqttDefaultFilePersistence( dir); MqttClient client = new MqttClient("tcp://127.0.0.1:1833","0001",mdfp); } catch (Exception e) { String errorText = ""; for (StackTraceElement error : e.getStackTrace()) errorText += error.toString() + "\n"; errorTextView.setText(errorText); } } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_mqtt_sample,menu); return true; } }
这是堆栈:
MqttException(0) at org.eclipse.paho.client.mqttv3.MqttDefaultFilePersistence.open(MqttDefaultFilePersistence.java:74) org.eclipse.paho.client.mqttv3.MqttClient.<init>(MqttClient.java:183) com.storassa.android.mqttsample.MqttSample$1.onClick(MqttSample.java:38) android.view.View.performClick(View.java:4202) android.view.View$PerformClick.run(View.java:17340) android.os.Handler.handleCallback(Handler.java:725) android.os.Handler.dispatchMessage(Handler.java:92) android.os.Looper.loop(Looper.java:137) android.app.ActivityThread.main(ActivityThread.java:5039) java.lang.reflect.Method.invokeNative(Native Method) java.lang.reflect.Method.invoke(Method.java:511) com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) dalvik.system.NativeStart.main(Native Method)
解决方法
如果不依赖文件持久性,也可以使用类MemoryPersistence的实例.
因此,获得客户的电话将是:
MemoryPersistence persistence = new MemoryPersistence(); MqttClient client = new MqttClient(host,clientId,persistence);