Again: Android MQTT cannot create client

I am trying to create an MqttClient in Android using the Android emulator (Nexus7) in ADT (Eclipse 3.8.0 on Linux Fedora17. I found the answer to this question (Android MQTT could not create the client) , but it could not solve my problem.

I created an action that allows the user to enter the directory in which the save file is stored, but there is no way to avoid the exception. I tried using "/ mnt / sdcard /", "/ mnt /", "/ mnt / sdcard / tmp /", etc.

Do I need to pay attention to certain settings in the Android emulator in the Eclipse project? Are there any permissions to be included in the application?

I looked at the various mqtt resources mentioned in the quoted answer, but I could not find the answer.

This is my code:

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; } } 

and this is the stack:

 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) 
+7
source share
5 answers

After a whole day spent understanding the problem and searching anywhere to find it, in the end I got it: very simple permission should be added to the manifest

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

I do not know if internal directories can be used instead of sdcard.

As I answer my question for the first time, let me know if this is etiquette in order to correctly determine the answer to my own answer (I found different opinions on these topics)

+5
source

Alternatively, you can use an instance of the MemoryPersistence class if you do not rely on file persistence.

So the call to get the client will be as follows:

 MemoryPersistence persistence = new MemoryPersistence(); MqttClient client = new MqttClient(host, clientId, persistence); 
+9
source

This solved my problem.

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

WAMP is also a little mocking.

0
source

The error is due to the MQTT client code trying to open a file on local storage to store messages when publishing at high (1/2) QOS levels. There will be 2 reasons for the error:

1. Perhaps you did not provide access to the storage for the application

2.You are unlikely to have permission to write to the default location that the MQTT client chooses.

The easiest way to get around this is to use the in-memory memory (org.eclipse.paho.client.mqttv3.persist.MemoryPersistence) for the MQTT client.

 ... MemoryPersistence persistence = new MemoryPersistence(); client = new MqttClient("tcp://test.mosquitto.org:1883", clientId); ... 

Once you get around this problem, you are likely to encounter the problem of creating an io network in the user interface thread. To avoid this, I suggest you look at the AsyncTask code that supports Android

0
source
  MqttClientPersistence persistence = new MqttDefaultFilePersistence(mContext.getApplicationInfo().dataDir); mqttclient = new MqttAsyncClient(url.toString(), clientId, persistence); 
-2
source

All Articles