0

EDIT I've add pars function with using Volley. Here it's function codes:

private void pars(){

    JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, String.valueOf(txt), null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    try {
                        JSONArray jsonArray = response.getJSONArray("employees");
                        for(int i = 0; i<jsonArray.length(); i++){
                            JSONObject employee = jsonArray.getJSONObject(i);
                            String firstName = employee.getString("firstname");
                            int age = employee.getInt("age");
                            String mail = employee.getString("mail");
                            txt.append(firstName + ", " + String.valueOf(age) + ", " + mail + "\n\n");
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();
        }
    });
     mQueue.add(request);}

As you can see, i wrote "String.valueOf(txt) for URL side. But it doesn't work now. "new Response.Listener() becomes to gray after i add this txt code.

---------END OF EDIT----------------

I have a problem with parsing JSON data which is a coming from MQTT. I'm using PAHO MQTT Server for connect and subscribe to my host and topic in android studio.

I'm taking topic data with textInput for where user wants to be subscribe, just like that:

               topic = topicInput.getEditText().getText().toString();

And after subscribe a topic, i'll show the messages in TextView, here's how:

                txt.setText(new String(message.getPayload()));

Theese codes working fine for take messages from MQTT. But, when i push JSON data from mqtt, my textView doesn't show parsed data. How can i parse this string? I've search every website, they usally takes normal url to parse a json data. But there is some different problem. I don't have "normal" url, i have a topic.

How can i do that?

Here is my full code of MainActivity.java:

public class MainActivity extends AppCompatActivity {
private Button btn1;
private Button btn2;
private TextView txt;
private static final String TAG="MyTag";
private MqttAndroidClient client;
private TextInputLayout topicInput;


@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    

    btn1 = (Button) findViewById(R.id.btn1);
    btn2 = (Button) findViewById(R.id.btn2);
    txt = (TextView)findViewById(R.id.txt);
    topicInput = (TextInputLayout) findViewById(R.id.topicInput);

    String clientId = MqttClient.generateClientId();

    final  MqttAndroidClient client =
            new MqttAndroidClient(MainActivity.this, "tcp://broker.mqttdashboard.com:1883",
                    clientId);

    //Connection
    btn1.setOnClickListener(new View.OnClickListener() {
        AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
        @Override
        public void onClick(View v) {

            try {
                IMqttToken token = client.connect();
                token.setActionCallback(new IMqttActionListener() {
                    @Override
                    public void onSuccess(IMqttToken asyncActionToken) {
                        Toast.makeText(MainActivity.this, "MQTT Bağlantısı Kuruldu", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
                        Toast.makeText(MainActivity.this, "MQTT Bağlantısı BAŞARISIZ!", Toast.LENGTH_SHORT).show();
                    }
                });
            } catch (MqttException e) {
                e.printStackTrace();
            }
        }

    });

    //Subscribe settings
    btn2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String topic;
           topic = topicInput.getEditText().getText().toString();


            int qos = 1;
            try {
                IMqttToken subToken = client.subscribe(topic, qos);
                subToken.setActionCallback(new IMqttActionListener() {
                    @Override
                    public void onSuccess(IMqttToken asyncActionToken) {
                        Toast.makeText(MainActivity.this, "Topic'e Bağlanıldı", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onFailure(IMqttToken asyncActionToken,
                                          Throwable exception) {
                        Toast.makeText(MainActivity.this, "Topic'e Bağlantı Kurulamadı", Toast.LENGTH_SHORT).show();


                    }
                });
            } catch (MqttException e) {
                e.printStackTrace();
            }

        }
    });

    //Text
    client.setCallback(new MqttCallback() {
        @Override
        public void connectionLost(Throwable cause) {

        }

        @Override
        public void messageArrived(String topic, MqttMessage message) throws Exception {
            txt.setText(new String(message.getPayload()));
        }
        @Override
        public void deliveryComplete(IMqttDeliveryToken token) {

        }
    });
}

}

  • What do you mean by `TextView won't parse if`? A textView will just display the text it is given, it won't do any conversion/processing/parsing on it. – hardillb Jul 14 '21 at 14:50
  • @hardillb i mean my textview doesn't show the "parsed" data. I know textView cant process. – CreativeEngineer Jul 14 '21 at 14:54

0 Answers0