How can I use Java to connect to/embed the ThingSpeak server (API) in our own Android studio app?

111 views Asked by At

How can our own custom app read data from the thingSpeak server using the thingSpeak API?

As I have seen from other resources, the code I tried to use to connect my application to the thingSpeak server contained numerous errors, which prevented me from succeeding. I want to use my application to connect to the ThingSpeak server, but I keep getting errors.

The following permissions have been added to the app's manifest file:

'<uses-permission android:name="android.permission.INTERNET"/>'

 [Internet permission code] [1]

Then, incorporate the Volley library into the build.gradle file for the project and sync it: [Volley library code ] 2`

'implementation 'com.android.volley:volley:1.2.1'

added all volleyball libraries to the request: [Imported all the libraries] [3]

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

After that i have created a new ACTIVITY 5 and write cide there:`package com.example.simp;

import android.os.Bundle;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

public class MainActivity5 extends AppCompatActivity {
    private static final String fd = MainActivity5.class.getSimpleName();
    private String url = "https://api.thingspeak.com/channels/ 2046669/feeds.json?api_key=ABCABCABACXYZ&results=10";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main5);
        // Instantiate the RequestQueue
        RequestQueue queue = Volley.newRequestQueue(this);
Request a string response from the provided URL.
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
                new Response. Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        // Display the response string.
                        Log.d(fd, "Response: " + response);
                        // Parse the response string and display the data in your app's UI.
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        // Handle error
                        Log.e(fd, "Error: " + error.toString());
                    }
                });
        // Add the request to the RequestQueue
        queue.add(stringRequest);
    }
}

[Code with libraries] 4 [code with fetch request] 5

0

There are 0 answers