Android Studio - MQTT to JSON











up vote
0
down vote

favorite












I am struggling to send/receive the MQTT messages as JSON.



This is my code:



public class MainActivity extends AppCompatActivity {
Gson gson = new Gson();

public static final String BROKER_URL = "tcp://iot.eclipse.org:1883";


String userid = "12345"; //
//We have to generate a unique Client id.
String clientId = userid + "-sub";

// Default sensor to listen for -
// Change to another if you are broadcasting a different sensor name
String sensorname = "brightness";
String sensorTemp = "temperature";

String topicname = userid + "/" + sensorname;
String topicname2 = userid + "/" + sensorTemp;
public final String TOPIC_BRIGHTNESS = userid + "/brightness";
public final String TOPIC_TEMPERATURE = userid + "/temperature";
public final String TOPIC_DOORLOCK = userid + "/doorlock";
private MqttClient mqttClient;
Button publishTemperatureBtn;
Switch publishDoorState;
SeekBar tempSeekBar;
TextView tempLabel;

SensorData oneSensor = new SensorData("unknown", "unknown");
String oneSensorJson = new String();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
publishTemperatureBtn = (Button) findViewById(R.id.publishTemperatureBtnID);
tempSeekBar = (SeekBar) findViewById(R.id.temperatureSeekBar);
publishDoorState = (Switch) findViewById(R.id.doorSwitch);
tempLabel = (TextView) findViewById(R.id.temperatureLabel);

publishTemperatureBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Code here executes on main thread after user presses button
System.out.println("PUBLISHING");
runOnUiThread(new Runnable() {
public void run() {
publishTemperature();
}
});
}
});

publishDoorState.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if (isChecked == true) {
runOnUiThread(new Runnable() {
@Override
public void run() {
publishDoorStateMethod_ON();
}
});
} else if (isChecked == false) {
runOnUiThread(new Runnable() {
@Override
public void run() {
publishDoorStateMethod_OFF();
}
});
}
}
});


tempSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
tempLabel.setText(i + "°C");
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
runOnUiThread(new Runnable() {
@Override
public void run() {
publishTemperature();
}
});
}
});


// start new
// Create MQTT client and start subscribing to message queue
try {
// change from original. Messages in "null" are not stored
mqttClient = new MqttClient(BROKER_URL, clientId, null);
mqttClient.setCallback(new MqttCallbackExtended() {
@Override
public void connectionLost(Throwable cause) {
//This is called when the connection is lost. We could reconnect here.
}

@Override
public void messageArrived(final String topic, final MqttMessage message) throws Exception {
System.out.println("DEBUG: Message arrived. Topic: " + topic + " Message: " + message.toString());
// get message data

final String messageStr = message.toString();

runOnUiThread(new Runnable() {
public void run() {
System.out.println("Updating UI");
// Update UI elements

if ((topic).equals(topicname)) {
System.err.println("Sensor gone!");
TextView sensorValueTV = (TextView) findViewById(R.id.sensorValueTV);
sensorValueTV.setText(messageStr);
} else if ((topic).equals(topicname2)) {
System.err.println("Sensor gone!");
TextView sensorTempValue = (TextView) findViewById(R.id.sensorTempValue);
sensorTempValue.setText(messageStr);
}


}
});

}

@Override
public void deliveryComplete(IMqttDeliveryToken token) {
//no-op
}

@Override
public void connectComplete(boolean b, String s) {
//no-op
}
});
} catch (MqttException e) {
e.printStackTrace();
System.exit(1);
}

// temp use of ThreadPolicy until use AsyncTask
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

startSubscribing();


}


public void startSubscribing() {
try {
mqttClient.connect();

//Subscribe to all subtopics of home
final String topic = topicname;
mqttClient.subscribe(topic);
final String topic2 = topicname2;
mqttClient.subscribe(topic2);

System.out.println("Subscriber is now listening to " + topic);

} catch (MqttException e) {
e.printStackTrace();
System.exit(1);
}
}

public void publishTemperature() {
try {
final MqttTopic temperatureTopic = mqttClient.getTopic(TOPIC_TEMPERATURE);
final int temperatureNumber = 30;
EditText temperatureETValue = (EditText) findViewById(R.id.temperatureETValue);
final String temperature = tempLabel.getText().toString();


temperatureTopic.publish(new MqttMessage(temperature.getBytes()));

System.out.println("Now publishing " + temperatureTopic);

} catch (MqttException e) {
e.printStackTrace();
System.exit(1);
}
}

public void publishDoorStateMethod_ON() {
try {
final MqttTopic doorButtonTopic = mqttClient.getTopic(TOPIC_DOORLOCK);
String doorState;
Button doorStateButton = (Button) findViewById(R.id.doorSwitch);
doorState = "ON";
doorButtonTopic.publish(new MqttMessage(doorState.getBytes()));

System.out.println("Now publishing " + doorButtonTopic);
} catch (MqttException e) {
e.printStackTrace();
System.exit(1);
}
}

public void publishDoorStateMethod_OFF() {
try {
final MqttTopic doorButtonTopic = mqttClient.getTopic(TOPIC_DOORLOCK);
String doorState;
Button doorStateButton = (Button) findViewById(R.id.doorSwitch);
doorState = "OFF";
doorButtonTopic.publish(new MqttMessage(doorState.getBytes()));

System.out.println("Now publishing " + doorButtonTopic);


} catch (MqttException e) {
e.printStackTrace();
System.exit(1);
}
}
}


This is what I have been tasked to do:




  1. Amend the Android code to send/receive the MQTT messages as json strings representing sensor objects. You will need to include the same SensorData object that you used in the Eclipse labs, and include the Gson class in your Android project. Use sample code from the Eclipse client/server labs to remind you how to create json objects from the SensorData object, and how to extract the object data when a json object arrives.


I already have my SensorData object. The examples in Elicpse that I have are no help whatsoever. Could someone please point me in the right direction.










share|improve this question






















  • We won't do your homework for you. You need to be a lot more specific about what you need help with.
    – hardillb
    Nov 22 at 7:25















up vote
0
down vote

favorite












I am struggling to send/receive the MQTT messages as JSON.



This is my code:



public class MainActivity extends AppCompatActivity {
Gson gson = new Gson();

public static final String BROKER_URL = "tcp://iot.eclipse.org:1883";


String userid = "12345"; //
//We have to generate a unique Client id.
String clientId = userid + "-sub";

// Default sensor to listen for -
// Change to another if you are broadcasting a different sensor name
String sensorname = "brightness";
String sensorTemp = "temperature";

String topicname = userid + "/" + sensorname;
String topicname2 = userid + "/" + sensorTemp;
public final String TOPIC_BRIGHTNESS = userid + "/brightness";
public final String TOPIC_TEMPERATURE = userid + "/temperature";
public final String TOPIC_DOORLOCK = userid + "/doorlock";
private MqttClient mqttClient;
Button publishTemperatureBtn;
Switch publishDoorState;
SeekBar tempSeekBar;
TextView tempLabel;

SensorData oneSensor = new SensorData("unknown", "unknown");
String oneSensorJson = new String();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
publishTemperatureBtn = (Button) findViewById(R.id.publishTemperatureBtnID);
tempSeekBar = (SeekBar) findViewById(R.id.temperatureSeekBar);
publishDoorState = (Switch) findViewById(R.id.doorSwitch);
tempLabel = (TextView) findViewById(R.id.temperatureLabel);

publishTemperatureBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Code here executes on main thread after user presses button
System.out.println("PUBLISHING");
runOnUiThread(new Runnable() {
public void run() {
publishTemperature();
}
});
}
});

publishDoorState.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if (isChecked == true) {
runOnUiThread(new Runnable() {
@Override
public void run() {
publishDoorStateMethod_ON();
}
});
} else if (isChecked == false) {
runOnUiThread(new Runnable() {
@Override
public void run() {
publishDoorStateMethod_OFF();
}
});
}
}
});


tempSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
tempLabel.setText(i + "°C");
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
runOnUiThread(new Runnable() {
@Override
public void run() {
publishTemperature();
}
});
}
});


// start new
// Create MQTT client and start subscribing to message queue
try {
// change from original. Messages in "null" are not stored
mqttClient = new MqttClient(BROKER_URL, clientId, null);
mqttClient.setCallback(new MqttCallbackExtended() {
@Override
public void connectionLost(Throwable cause) {
//This is called when the connection is lost. We could reconnect here.
}

@Override
public void messageArrived(final String topic, final MqttMessage message) throws Exception {
System.out.println("DEBUG: Message arrived. Topic: " + topic + " Message: " + message.toString());
// get message data

final String messageStr = message.toString();

runOnUiThread(new Runnable() {
public void run() {
System.out.println("Updating UI");
// Update UI elements

if ((topic).equals(topicname)) {
System.err.println("Sensor gone!");
TextView sensorValueTV = (TextView) findViewById(R.id.sensorValueTV);
sensorValueTV.setText(messageStr);
} else if ((topic).equals(topicname2)) {
System.err.println("Sensor gone!");
TextView sensorTempValue = (TextView) findViewById(R.id.sensorTempValue);
sensorTempValue.setText(messageStr);
}


}
});

}

@Override
public void deliveryComplete(IMqttDeliveryToken token) {
//no-op
}

@Override
public void connectComplete(boolean b, String s) {
//no-op
}
});
} catch (MqttException e) {
e.printStackTrace();
System.exit(1);
}

// temp use of ThreadPolicy until use AsyncTask
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

startSubscribing();


}


public void startSubscribing() {
try {
mqttClient.connect();

//Subscribe to all subtopics of home
final String topic = topicname;
mqttClient.subscribe(topic);
final String topic2 = topicname2;
mqttClient.subscribe(topic2);

System.out.println("Subscriber is now listening to " + topic);

} catch (MqttException e) {
e.printStackTrace();
System.exit(1);
}
}

public void publishTemperature() {
try {
final MqttTopic temperatureTopic = mqttClient.getTopic(TOPIC_TEMPERATURE);
final int temperatureNumber = 30;
EditText temperatureETValue = (EditText) findViewById(R.id.temperatureETValue);
final String temperature = tempLabel.getText().toString();


temperatureTopic.publish(new MqttMessage(temperature.getBytes()));

System.out.println("Now publishing " + temperatureTopic);

} catch (MqttException e) {
e.printStackTrace();
System.exit(1);
}
}

public void publishDoorStateMethod_ON() {
try {
final MqttTopic doorButtonTopic = mqttClient.getTopic(TOPIC_DOORLOCK);
String doorState;
Button doorStateButton = (Button) findViewById(R.id.doorSwitch);
doorState = "ON";
doorButtonTopic.publish(new MqttMessage(doorState.getBytes()));

System.out.println("Now publishing " + doorButtonTopic);
} catch (MqttException e) {
e.printStackTrace();
System.exit(1);
}
}

public void publishDoorStateMethod_OFF() {
try {
final MqttTopic doorButtonTopic = mqttClient.getTopic(TOPIC_DOORLOCK);
String doorState;
Button doorStateButton = (Button) findViewById(R.id.doorSwitch);
doorState = "OFF";
doorButtonTopic.publish(new MqttMessage(doorState.getBytes()));

System.out.println("Now publishing " + doorButtonTopic);


} catch (MqttException e) {
e.printStackTrace();
System.exit(1);
}
}
}


This is what I have been tasked to do:




  1. Amend the Android code to send/receive the MQTT messages as json strings representing sensor objects. You will need to include the same SensorData object that you used in the Eclipse labs, and include the Gson class in your Android project. Use sample code from the Eclipse client/server labs to remind you how to create json objects from the SensorData object, and how to extract the object data when a json object arrives.


I already have my SensorData object. The examples in Elicpse that I have are no help whatsoever. Could someone please point me in the right direction.










share|improve this question






















  • We won't do your homework for you. You need to be a lot more specific about what you need help with.
    – hardillb
    Nov 22 at 7:25













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am struggling to send/receive the MQTT messages as JSON.



This is my code:



public class MainActivity extends AppCompatActivity {
Gson gson = new Gson();

public static final String BROKER_URL = "tcp://iot.eclipse.org:1883";


String userid = "12345"; //
//We have to generate a unique Client id.
String clientId = userid + "-sub";

// Default sensor to listen for -
// Change to another if you are broadcasting a different sensor name
String sensorname = "brightness";
String sensorTemp = "temperature";

String topicname = userid + "/" + sensorname;
String topicname2 = userid + "/" + sensorTemp;
public final String TOPIC_BRIGHTNESS = userid + "/brightness";
public final String TOPIC_TEMPERATURE = userid + "/temperature";
public final String TOPIC_DOORLOCK = userid + "/doorlock";
private MqttClient mqttClient;
Button publishTemperatureBtn;
Switch publishDoorState;
SeekBar tempSeekBar;
TextView tempLabel;

SensorData oneSensor = new SensorData("unknown", "unknown");
String oneSensorJson = new String();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
publishTemperatureBtn = (Button) findViewById(R.id.publishTemperatureBtnID);
tempSeekBar = (SeekBar) findViewById(R.id.temperatureSeekBar);
publishDoorState = (Switch) findViewById(R.id.doorSwitch);
tempLabel = (TextView) findViewById(R.id.temperatureLabel);

publishTemperatureBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Code here executes on main thread after user presses button
System.out.println("PUBLISHING");
runOnUiThread(new Runnable() {
public void run() {
publishTemperature();
}
});
}
});

publishDoorState.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if (isChecked == true) {
runOnUiThread(new Runnable() {
@Override
public void run() {
publishDoorStateMethod_ON();
}
});
} else if (isChecked == false) {
runOnUiThread(new Runnable() {
@Override
public void run() {
publishDoorStateMethod_OFF();
}
});
}
}
});


tempSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
tempLabel.setText(i + "°C");
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
runOnUiThread(new Runnable() {
@Override
public void run() {
publishTemperature();
}
});
}
});


// start new
// Create MQTT client and start subscribing to message queue
try {
// change from original. Messages in "null" are not stored
mqttClient = new MqttClient(BROKER_URL, clientId, null);
mqttClient.setCallback(new MqttCallbackExtended() {
@Override
public void connectionLost(Throwable cause) {
//This is called when the connection is lost. We could reconnect here.
}

@Override
public void messageArrived(final String topic, final MqttMessage message) throws Exception {
System.out.println("DEBUG: Message arrived. Topic: " + topic + " Message: " + message.toString());
// get message data

final String messageStr = message.toString();

runOnUiThread(new Runnable() {
public void run() {
System.out.println("Updating UI");
// Update UI elements

if ((topic).equals(topicname)) {
System.err.println("Sensor gone!");
TextView sensorValueTV = (TextView) findViewById(R.id.sensorValueTV);
sensorValueTV.setText(messageStr);
} else if ((topic).equals(topicname2)) {
System.err.println("Sensor gone!");
TextView sensorTempValue = (TextView) findViewById(R.id.sensorTempValue);
sensorTempValue.setText(messageStr);
}


}
});

}

@Override
public void deliveryComplete(IMqttDeliveryToken token) {
//no-op
}

@Override
public void connectComplete(boolean b, String s) {
//no-op
}
});
} catch (MqttException e) {
e.printStackTrace();
System.exit(1);
}

// temp use of ThreadPolicy until use AsyncTask
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

startSubscribing();


}


public void startSubscribing() {
try {
mqttClient.connect();

//Subscribe to all subtopics of home
final String topic = topicname;
mqttClient.subscribe(topic);
final String topic2 = topicname2;
mqttClient.subscribe(topic2);

System.out.println("Subscriber is now listening to " + topic);

} catch (MqttException e) {
e.printStackTrace();
System.exit(1);
}
}

public void publishTemperature() {
try {
final MqttTopic temperatureTopic = mqttClient.getTopic(TOPIC_TEMPERATURE);
final int temperatureNumber = 30;
EditText temperatureETValue = (EditText) findViewById(R.id.temperatureETValue);
final String temperature = tempLabel.getText().toString();


temperatureTopic.publish(new MqttMessage(temperature.getBytes()));

System.out.println("Now publishing " + temperatureTopic);

} catch (MqttException e) {
e.printStackTrace();
System.exit(1);
}
}

public void publishDoorStateMethod_ON() {
try {
final MqttTopic doorButtonTopic = mqttClient.getTopic(TOPIC_DOORLOCK);
String doorState;
Button doorStateButton = (Button) findViewById(R.id.doorSwitch);
doorState = "ON";
doorButtonTopic.publish(new MqttMessage(doorState.getBytes()));

System.out.println("Now publishing " + doorButtonTopic);
} catch (MqttException e) {
e.printStackTrace();
System.exit(1);
}
}

public void publishDoorStateMethod_OFF() {
try {
final MqttTopic doorButtonTopic = mqttClient.getTopic(TOPIC_DOORLOCK);
String doorState;
Button doorStateButton = (Button) findViewById(R.id.doorSwitch);
doorState = "OFF";
doorButtonTopic.publish(new MqttMessage(doorState.getBytes()));

System.out.println("Now publishing " + doorButtonTopic);


} catch (MqttException e) {
e.printStackTrace();
System.exit(1);
}
}
}


This is what I have been tasked to do:




  1. Amend the Android code to send/receive the MQTT messages as json strings representing sensor objects. You will need to include the same SensorData object that you used in the Eclipse labs, and include the Gson class in your Android project. Use sample code from the Eclipse client/server labs to remind you how to create json objects from the SensorData object, and how to extract the object data when a json object arrives.


I already have my SensorData object. The examples in Elicpse that I have are no help whatsoever. Could someone please point me in the right direction.










share|improve this question













I am struggling to send/receive the MQTT messages as JSON.



This is my code:



public class MainActivity extends AppCompatActivity {
Gson gson = new Gson();

public static final String BROKER_URL = "tcp://iot.eclipse.org:1883";


String userid = "12345"; //
//We have to generate a unique Client id.
String clientId = userid + "-sub";

// Default sensor to listen for -
// Change to another if you are broadcasting a different sensor name
String sensorname = "brightness";
String sensorTemp = "temperature";

String topicname = userid + "/" + sensorname;
String topicname2 = userid + "/" + sensorTemp;
public final String TOPIC_BRIGHTNESS = userid + "/brightness";
public final String TOPIC_TEMPERATURE = userid + "/temperature";
public final String TOPIC_DOORLOCK = userid + "/doorlock";
private MqttClient mqttClient;
Button publishTemperatureBtn;
Switch publishDoorState;
SeekBar tempSeekBar;
TextView tempLabel;

SensorData oneSensor = new SensorData("unknown", "unknown");
String oneSensorJson = new String();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
publishTemperatureBtn = (Button) findViewById(R.id.publishTemperatureBtnID);
tempSeekBar = (SeekBar) findViewById(R.id.temperatureSeekBar);
publishDoorState = (Switch) findViewById(R.id.doorSwitch);
tempLabel = (TextView) findViewById(R.id.temperatureLabel);

publishTemperatureBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Code here executes on main thread after user presses button
System.out.println("PUBLISHING");
runOnUiThread(new Runnable() {
public void run() {
publishTemperature();
}
});
}
});

publishDoorState.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if (isChecked == true) {
runOnUiThread(new Runnable() {
@Override
public void run() {
publishDoorStateMethod_ON();
}
});
} else if (isChecked == false) {
runOnUiThread(new Runnable() {
@Override
public void run() {
publishDoorStateMethod_OFF();
}
});
}
}
});


tempSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
tempLabel.setText(i + "°C");
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
runOnUiThread(new Runnable() {
@Override
public void run() {
publishTemperature();
}
});
}
});


// start new
// Create MQTT client and start subscribing to message queue
try {
// change from original. Messages in "null" are not stored
mqttClient = new MqttClient(BROKER_URL, clientId, null);
mqttClient.setCallback(new MqttCallbackExtended() {
@Override
public void connectionLost(Throwable cause) {
//This is called when the connection is lost. We could reconnect here.
}

@Override
public void messageArrived(final String topic, final MqttMessage message) throws Exception {
System.out.println("DEBUG: Message arrived. Topic: " + topic + " Message: " + message.toString());
// get message data

final String messageStr = message.toString();

runOnUiThread(new Runnable() {
public void run() {
System.out.println("Updating UI");
// Update UI elements

if ((topic).equals(topicname)) {
System.err.println("Sensor gone!");
TextView sensorValueTV = (TextView) findViewById(R.id.sensorValueTV);
sensorValueTV.setText(messageStr);
} else if ((topic).equals(topicname2)) {
System.err.println("Sensor gone!");
TextView sensorTempValue = (TextView) findViewById(R.id.sensorTempValue);
sensorTempValue.setText(messageStr);
}


}
});

}

@Override
public void deliveryComplete(IMqttDeliveryToken token) {
//no-op
}

@Override
public void connectComplete(boolean b, String s) {
//no-op
}
});
} catch (MqttException e) {
e.printStackTrace();
System.exit(1);
}

// temp use of ThreadPolicy until use AsyncTask
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

startSubscribing();


}


public void startSubscribing() {
try {
mqttClient.connect();

//Subscribe to all subtopics of home
final String topic = topicname;
mqttClient.subscribe(topic);
final String topic2 = topicname2;
mqttClient.subscribe(topic2);

System.out.println("Subscriber is now listening to " + topic);

} catch (MqttException e) {
e.printStackTrace();
System.exit(1);
}
}

public void publishTemperature() {
try {
final MqttTopic temperatureTopic = mqttClient.getTopic(TOPIC_TEMPERATURE);
final int temperatureNumber = 30;
EditText temperatureETValue = (EditText) findViewById(R.id.temperatureETValue);
final String temperature = tempLabel.getText().toString();


temperatureTopic.publish(new MqttMessage(temperature.getBytes()));

System.out.println("Now publishing " + temperatureTopic);

} catch (MqttException e) {
e.printStackTrace();
System.exit(1);
}
}

public void publishDoorStateMethod_ON() {
try {
final MqttTopic doorButtonTopic = mqttClient.getTopic(TOPIC_DOORLOCK);
String doorState;
Button doorStateButton = (Button) findViewById(R.id.doorSwitch);
doorState = "ON";
doorButtonTopic.publish(new MqttMessage(doorState.getBytes()));

System.out.println("Now publishing " + doorButtonTopic);
} catch (MqttException e) {
e.printStackTrace();
System.exit(1);
}
}

public void publishDoorStateMethod_OFF() {
try {
final MqttTopic doorButtonTopic = mqttClient.getTopic(TOPIC_DOORLOCK);
String doorState;
Button doorStateButton = (Button) findViewById(R.id.doorSwitch);
doorState = "OFF";
doorButtonTopic.publish(new MqttMessage(doorState.getBytes()));

System.out.println("Now publishing " + doorButtonTopic);


} catch (MqttException e) {
e.printStackTrace();
System.exit(1);
}
}
}


This is what I have been tasked to do:




  1. Amend the Android code to send/receive the MQTT messages as json strings representing sensor objects. You will need to include the same SensorData object that you used in the Eclipse labs, and include the Gson class in your Android project. Use sample code from the Eclipse client/server labs to remind you how to create json objects from the SensorData object, and how to extract the object data when a json object arrives.


I already have my SensorData object. The examples in Elicpse that I have are no help whatsoever. Could someone please point me in the right direction.







android json mqtt






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 at 23:55









TheApprentice

102




102












  • We won't do your homework for you. You need to be a lot more specific about what you need help with.
    – hardillb
    Nov 22 at 7:25


















  • We won't do your homework for you. You need to be a lot more specific about what you need help with.
    – hardillb
    Nov 22 at 7:25
















We won't do your homework for you. You need to be a lot more specific about what you need help with.
– hardillb
Nov 22 at 7:25




We won't do your homework for you. You need to be a lot more specific about what you need help with.
– hardillb
Nov 22 at 7:25

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53422088%2fandroid-studio-mqtt-to-json%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53422088%2fandroid-studio-mqtt-to-json%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks