ANDROID:SENDING DATA FROM ANDROID TO THINGSPEAK

This is the code to send data from Android device to Thinspeak server.I used a Textview,EditText and Button in this program.On button click ,the text entered in the editText is displayed in Text view and send to Thingspeak server.

import android.app.DownloadManager;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.StrictMode;
import android.preference.PreferenceActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import cz.msebera.android.httpclient.entity.mime.Header;


public class MainActivity extends AppCompatActivity {
    String str;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        Button button = (Button)findViewById(R.id.button1);




        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                EditText eText;
                eText = (EditText)findViewById(R.id.editText);
                str = eText.getText().toString();
                
                TextView myAwesomeTextView = (TextView)findViewById(R.id.textView);

//in your OnCreate() method
                myAwesomeTextView.setText(str);


                try {
                    new FetchThingspeakTask().execute();
                }
                catch(Exception e){
                    Log.e("ERROR", e.getMessage(), e);
                }
            }
        });
    }
    class FetchThingspeakTask extends AsyncTask<Void, Void, String> {
        protected void onPreExecute() {
        }

        protected String doInBackground(Void... urls) {
            try {

                String urlString="https://api.thingspeak.com/update?api_key=Z94T86Z72Q1VQT8M&field1="+str.toString();

                URL url = new URL(urlString);
                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                try {
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
                    StringBuilder stringBuilder = new StringBuilder();
                    String line;
                    while ((line = bufferedReader.readLine()) != null) {
                        stringBuilder.append(line).append("\n");
                    }
                    bufferedReader.close();
                    return stringBuilder.toString();
                } finally {
                    urlConnection.disconnect();
                }
            } catch (Exception e) {
                Log.e("ERROR", e.getMessage(), e);
                return null;
            }
        }
        protected void onPostExecute(String response) {
            if (response == null) {
                Toast.makeText(MainActivity.this, "There was an error", Toast.LENGTH_SHORT).show();

                return;
            } else {

                Toast.makeText(MainActivity.this, "Success.Please wait for verification", Toast.LENGTH_SHORT).show();
                return;

            }
        }
        // Code here executes on main thread after user presses button
    }

}

Leave a Reply

Your email address will not be published. Required fields are marked *