Post


Android HTTP Client Example

Complete MainActivity.java code with solution for http and location

 

Step 1)

add dependency:

implementation 'com.loopj.android:android-async-http:1.4.9' 

read documentation at
http://loopj.com/android-async-http/

 

Step 2) Add Code for Activity: 


public class MainActivity extends AppCompatActivity {
    AsyncHttpClient client;
    TextView tv;
    SeekBar sb;
    ProgressBar pb;
    String url = "http://172.168.22.15/control?lightno=1&state=";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btnHttp = findViewById(R.id.btnHttp);
        client = new AsyncHttpClient();
        tv = findViewById(R.id.txtv);
        sb = findViewById(R.id.seekBar);
        pb = findViewById(R.id.progressBar2);

        pb.setVisibility(View.GONE);

        sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            int val = 0;

            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                val = i;
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                pb.setVisibility(View.VISIBLE);
                RequestParams params = new RequestParams();
params.put("lightno", "1");//URL parameter in Object form params.put("state", val);
//http://172.168.22.15/control?lightno=1&state=85 client.get(url, params, new TextHttpResponseHandler() { @Override public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) { pb.setVisibility(View.GONE); } @Override public void onSuccess(int statusCode, Header[] headers, String responseString) { //Toast.makeText(MainActivity.this,"",Toast.LENGTH_LONG); Log.d("http", "happening" + responseString); tv.setText(responseString); pb.setVisibility(View.GONE); } }); } });
Button bt = findViewById(R.id.btnLocation);//Button for getting Location Data from GPS sensor bt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); LocationListener ll = new LocationListener() { @Override public void onLocationChanged(Location location) { double lt = location.getLatitude(); double ln = location.getLongitude(); tv.setText(lt+","+ln); } @Override public void onStatusChanged(String s, int i, Bundle bundle) { } @Override public void onProviderEnabled(String s) { } @Override public void onProviderDisabled(String s) { } }; if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll); return; } String lp = LocationManager.GPS_PROVIDER; Location lastLocation = lm.getLastKnownLocation(lp); double ltt=lastLocation.getLatitude(); double lon=lastLocation.getLongitude(); tv.setText(ltt+","+lon); Log.d("Location############ ",""+ltt); } }); btnHttp.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { pb.setVisibility(View.VISIBLE); RequestParams params = new RequestParams(); params.put("lightno","1"); params.put("state","56"); //http://172.168.22.15/control?lightno=1&state=1 client.get("http://www.iocare.in/testurl", params, new TextHttpResponseHandler() { @Override public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) { pb.setVisibility(View.GONE); } @Override public void onSuccess(int statusCode, Header[] headers, String responseString) { //Toast.makeText(MainActivity.this,"",Toast.LENGTH_LONG); Log.d("http","happening"+responseString); tv.setText(responseString); pb.setVisibility(View.GONE); } }); } }); } }


Step 3 ) Update your UI as per requirement of Code

UI needs 6 controls

1) Button to test AsyncHttp library http request to NodeMCU

2) Second Button to test Location service and get latitude and Longitude

4) SeekBar control to send dimming data to NodeMCU with AsyncHttp library

5) A progressbar Control to show progress of request.

6) A textView to hold some result data.

 

 

Posted in Arduino, Internet 11 Dec 2018