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 5 of 5

Thread: OkHttpRequest data retrieval need help

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

    Post OkHttpRequest data retrieval need help

    Hello,

    For the project I,am working on I have the following chalenge,


    With the help of a ESP32 and the BME280 sensor I have a server which gives the sensor data.
    No problem s far.
    But I would like to have the data in an app on my android mobile.

    The url page is converted to a tekst file with the help of the OkHttprequest methode which you can fins on you-tube works like a charm.
    In this page/file/string??? I can read the temp. I will retrieve from the file howe can this be done???

    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_result);

    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() {
    mTextViewResult.setText(myResponse);

    }

    });
    }


    }


    });
    }

    }

    As newbee I think this must be programmed in the listing/script as shown above.

    Any hints and help is welcome.

    Regards 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: OkHttpRequest data retrieval need help

    What happens when you compile, install and execute the code?
    must be programmed in the listing/script as shown above.
    What does that mean?

    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. The Following User Says Thank You to Norm For This Useful Post:

    ilioSS (December 3rd, 2019)

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

    Default Re: OkHttpRequest data retrieval need help

    Hello Norm,

    Thanks for your swift relpy.

    First the link for the OkHttprequest program android(app)
    https://www.youtube.com/watch?v=oGWJ8xD2W6k

    must be programmed in the listing/script as shown above. This means that to the best of my knowledge I have to add some program lines in order to retrieve the data. In the main activity.java . Is this correct
    If I compile and run on Android studio the app is installed on the Phone and gives the webpage in tekst. ( not possible to upload a pick of the webpage screen)


    This app reads a webserver page on my network with http://192.168.2.11 and make a myResponse file which is shown on the mobile Phone.
    This file contains the data sensor which I will retriev and make available for an other more readable format.


    This must be programmed in the java script of the app. listing below ( sorry if the format was not comply,s the forum rules)


    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_result);
     
            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() {
                                mTextViewResult.setText(myResponse);
     
                                 }
     
                        });
                    }
     
     
                    }
     
     
            });
        }
     
    }

    As Iam on my laptop it is not possible to enclose a screen shot of the tekst as shown on the mobile. This for more clarification the issue.

    btw howe to manage inserting photo,s in this message?

    Like to hear.

    Kind regards,
    ilioSS

  5. #4
    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: OkHttpRequest data retrieval need help

    For debugging the code you can use the System.out.println statement which will write to the logcat. The logcat can then be downloaded to a PC as a text file for examination.
    Or if the Android device is attached to the PC while testing the IDE should display what is on the logcat.

    Add several print statements to the code that print out the values of variables as they are given values, run the app and then copy the part of the logcat that shows what was printed.
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    ilioSS (December 5th, 2019)

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

    Default Re: OkHttpRequest data retrieval need help

    Hello Norm,

    I just did a quick reply but this will not reflect my apprecition of your help.

    I used after I found the logcat.
    This was of great help. Also some prints did there work and give me the info I needed.
    Also with the help of mu java for dummies in combination with your hints ans aswers I succed to have the sensor data retrieved from the string.
    This with index start and index stop.
    Now i,am able to use these data in my android app.

    Again many thanks.
    It was of great help. Nice to have this motivating help/hints.

    Kind regads,
    ilioSS

    ps. I will keep on studying on java/android

    For me this issue /question is closed

Similar Threads

  1. Substring Retrieval Method Question
    By Nuggets in forum Java Theory & Questions
    Replies: 12
    Last Post: January 31st, 2012, 12:16 AM
  2. XML InputSource class and the retrieval/browser type
    By dave0110 in forum Object Oriented Programming
    Replies: 2
    Last Post: December 3rd, 2010, 09:11 AM
  3. Content Based Image Retrieval
    By sanjay_jbp in forum AWT / Java Swing
    Replies: 9
    Last Post: March 1st, 2010, 08:55 PM
  4. Xml-Node Retrieval
    By prasb in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: December 4th, 2009, 12:44 PM
  5. MP3 Player: ID3 Tag Image Retrieval Problem for the Applet from internet
    By JavaJames in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 20th, 2009, 07:11 AM

Tags for this Thread