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