Android App To Command Standalone Esp8266/Esp-12 On-Board Led, Using Httpurlconnection

Posted on

This event accept the same usage of the in conclusion post service “Android App to command Standalone ESP8266/ESP-12 on-board LED, using HttpClient“, simply using java.net.HttpURLConnection instead of using Android 6.0 removed org.apache.http; to command the on-board inwards the post “Arduino-er: Standalone ESP8266/ESP-12: spider web command on-board LED“.

APK link tin endure constitute on bottom of this post.


MainActivity.java

package com.blogspot.android_er.androidespwebled;  import android.os.AsyncTask; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView;  import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL;  populace shape MainActivity extends AppCompatActivity {      EditText editIp;     Button btnOn, btnOff;     TextView textInfo1, textInfo2;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          editIp = (EditText)findViewById(R.id.ip);         btnOn = (Button)findViewById(R.id.bon);         btnOff = (Button)findViewById(R.id.boff);         textInfo1 = (TextView)findViewById(R.id.info1);         textInfo2 = (TextView)findViewById(R.id.info2);          btnOn.setOnClickListener(btnOnOffClickListener);         btnOff.setOnClickListener(btnOnOffClickListener);     }      View.OnClickListener btnOnOffClickListener = novel View.OnClickListener(){         @Override         populace void onClick(View v) {             String onoff;             if(v==btnOn){                 onoff="/on";             }else{                 onoff="/off";             }              btnOn.setEnabled(false);             btnOff.setEnabled(false);              String serverIP = editIp.getText().toString()+ onoff;              TaskEsp taskEsp = novel TaskEsp(serverIP);             taskEsp.execute();          }     };      mortal shape TaskEsp extends AsyncTask<Void, Void, String> {          String server;          TaskEsp(String server){             this.server = server;         }          @Override         protected String doInBackground(Void... params) {              in conclusion String p = "http://"+server;              runOnUiThread(new Runnable(){                 @Override                 populace void run() {                     textInfo1.setText(p);                 }             });              String serverResponse = "";              //Using java.net.HttpURLConnection             seek {                 HttpURLConnection httpURLConnection = (HttpURLConnection)(new URL(p).openConnection());                  if(httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {                      InputStream inputStream = null;                     inputStream = httpURLConnection.getInputStream();                     BufferedReader bufferedReader =                             novel BufferedReader(new InputStreamReader(inputStream));                     serverResponse = bufferedReader.readLine();                      inputStream.close();                 }             } grab (MalformedURLException e) {                 e.printStackTrace();                 serverResponse = e.getMessage();             } grab (IOException e) {                 e.printStackTrace();                 serverResponse = e.getMessage();             }             //              //Using org.apache.http             /*             HttpClient httpclient = novel DefaultHttpClient();             seek {                 HttpGet httpGet = novel HttpGet();                 httpGet.setURI(new URI(p));                 HttpResponse httpResponse = httpclient.execute(httpGet);                  InputStream inputStream = null;                 inputStream = httpResponse.getEntity().getContent();                 BufferedReader bufferedReader =                         novel BufferedReader(new InputStreamReader(inputStream));                 serverResponse = bufferedReader.readLine();                  inputStream.close();             } grab (URISyntaxException e) {                 e.printStackTrace();                 serverResponse = e.getMessage();             } grab (ClientProtocolException e) {                 e.printStackTrace();                 serverResponse = e.getMessage();             } grab (IOException e) {                 e.printStackTrace();                 serverResponse = e.getMessage();             }             */              supply serverResponse;         }          @Override         protected void onPostExecute(String s) {             textInfo2.setText(s);             btnOn.setEnabled(true);             btnOff.setEnabled(true);         }     } }  

For layout file, cite to in conclusion post service “Android App to command Standalone ESP8266/ESP-12 on-board LED, using HttpClient“.

uses-permission of “android.permission.INTERNET” is needed inwards AndroidManifest.xml.

NO demand to add together the code useLibrary ‘org.apache.http.legacy’ to build.gradle file.

 This event accept the same usage of the in conclusion post service  Android App to command Standalone ESP8266/ESP-12 on-board LED, using HttpURLConnectionDownload the files (Android Studio Format) .

 This event accept the same usage of the in conclusion post service  Android App to command Standalone ESP8266/ESP-12 on-board LED, using HttpURLConnectionDownload APK .

ESP8266/ESP-12 code, cite “Arduino-er: Standalone ESP8266/ESP-12: spider web command on-board LED“.