Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Display string and/or sensor values

  1. #1
    Junior Member
    Join Date
    Dec 2019
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question Display string and/or sensor values

    Good day,

    During my study on java/android/esp32 I found it difficult to have my sensor values displayed on my mobile screen.

    See my listing.
    It appeared that only the last value is displayed on the screen.
    My wish is to have these values all printed on the screen . Tempbu, tempin, humidity amd air press.


    [quote]
    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayo ut xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">



    <TextView
    android:id="@+id/text_view_tempbu"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="12dp"
    android:layout_marginLeft="12dp"
    android:layout_marginTop="32dp"
    android:layout_marginEnd="232dp"
    android:layout_marginRight="232dp"
    android:layout_marginBottom="694dp"
    android:text="tempbu"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.338"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.0" />

    <TextView
    android:id="@+id/text_view_tempin"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="32dp"
    android:layout_marginLeft="32dp"
    android:layout_marginTop="52dp"
    android:layout_marginEnd="232dp"
    android:layout_marginRight="232dp"
    android:layout_marginBottom="694dp"
    android:text="tempin"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.100000024" />
    [unquote]

    [quote]
    <?xml version="1.0" encoding="UTF-8" ?>
    <resources>
    <string name="app_name">OkHttprequestexample</string>
    <string name="tempbu">tempbu</string>
    <string name="tempin">tempin</string>
    <string name="hum">hum</string>
    <string name="pres">pres</string>
    </resources>
    [unquote]

    [quote]
    package com.example.okhttprequestexample;

    import androidx.appcompat.app.AppCompatActivity;
    import android.os.Bundle;
    import android.widget.TextView;
    import java.lang.String;
    import java.io.IOException;
    import okhttp3.Call;
    import okhttp3.Callback;
    import okhttp3.OkHttpClient;
    import okhttp3.Request;
    import okhttp3.Response;


    public class MainActivity extends AppCompatActivity {

    private TextView mTextViewResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mTextViewResult = findViewById(R.id.text_view_tempbu);

    OkHttpClient client = new OkHttpClient();

    String url = "http://192.168.2.11";

    Request request = new Request.Builder()
    .url(url)
    .build();

    client.newCall(request).enqueue(new Callback() {
    @Override
    public void onFailure(Call call, IOException e) {
    e.printStackTrace();
    }

    @Override
    public void onResponse(Call call, Response response) throws IOException {
    if (response.isSuccessful()) {
    final String myResponse = response.body().string();

    MainActivity.this.runOnUiThread(new Runnable() {
    @Override
    public void run() {

    // Temperatuur buiten ref. Temperatuur voor index

    int index_tempbu = myResponse.indexOf("Temperature");
    // System.out.println("index of substring" + index_tempbu);
    String tempbu = myResponse.substring(index_tempbu - 102, index_tempbu - 97);
    // System.out.println(myResponse.substring(index_temp bu - 102, index_tempbu - 97));
    mTextViewResult.setText(myResponse.subSequence(ind ex_tempbu - 102, index_tempbu - 97));
    // mTextViewResult.setText(tempbu);


    // Temperatuur binnen tempin ref. Humidity voor index

    int index_tempin = myResponse.indexOf("Humidity");
    // System.out.println("index of substring" + index_tempin);
    String tempin = myResponse.substring(index_tempin - 98, index_tempin - 93);
    // System.out.println(myResponse.substring(index_temp in - 98, index_tempin - 93));
    // mTextViewResult.setText(myResponse.subSequence(ind ex_tempin - 98, index_tempin - 93));
    // mTextViewResult.setText(tempin);


    // Humidity

    // int index_hum = myResponse.indexOf("&#");
    // System.out.println("index of substring" + index_hum);
    // String hum = myResponse.substring(index_hum - 37, index_hum - 32);
    // System.out.println(myResponse.substring(index_hum - 37, index_hum - 32));
    // mTextViewResult.setText(myResponse.subSequence(ind ex_hum - 37, index_hum - 32));
    // mTextViewResult.setText(hum);


    // Luchtdruk

    // int index_pres = myResponse.indexOf("hPa");
    // System.out.println("index of substring" + index_pres);
    // String pres = myResponse.substring(index_pres - 39, index_pres - 32);
    // System.out.println(myResponse.substring(index_pres - 39, index_pres - 32));
    // mTextViewResult.setText(myResponse.subSequence(ind ex_pres - 39, index_pres - 32));
    // mTextViewResult.setText(pres);


    }

    });


    }

    }

    });

    }

    [unquote]

    my screen looks like

    5.00 which is the buitentemp

    tempin

    hum
    pres

    The last three will not show there figures???


    Any answer which help is welcome.

    Like to hear,
    ilioSS

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Display string and/or sensor values

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2019
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Display string and/or sensor values

    Hello,

    Solved this question.
    I made for each sensor data a private class.
    Now it is possible to display the valeus.
    Kind regards,
    ilioSS

Similar Threads

  1. dynamically fetch values from database and display in combo box in jsp
    By Ritika Rishi in forum JavaServer Pages: JSP & JSTL
    Replies: 7
    Last Post: November 29th, 2013, 10:28 AM
  2. Replies: 2
    Last Post: October 26th, 2013, 09:56 AM
  3. Replies: 2
    Last Post: October 26th, 2013, 09:56 AM
  4. Display database values into JTables
    By tommyacton in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 13th, 2013, 02:54 PM
  5. A screen touch sensor
    By Someonation in forum Android Development
    Replies: 1
    Last Post: November 7th, 2011, 06:10 AM

Tags for this Thread