In this code, I explain how to send data in json format to server. It uses "RequestParams" to send data to server and get response from server in json format in "JSONObject response".
///////Getting location first///////////
Location location;
private Location getLocation() {
if (isNetworkAvailable( this )) {
LocationManager locationManager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );
if (locationManager != null) {
boolean gps_enabled = locationManager.isProviderEnabled( LocationManager.GPS_PROVIDER );
boolean network_enabled = locationManager.isProviderEnabled( LocationManager.NETWORK_PROVIDER );
if (ContextCompat.checkSelfPermission( NearMeActivity.this, android.Manifest.permission.ACCESS_FINE_LOCATION ) != PackageManager.PERMISSION_GRANTED) {
Toast.makeText( NearMeActivity.this, "Turn on location permission", Toast.LENGTH_SHORT ).show();
} else {
if (network_enabled) {
locationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, 5000, 10, this );
location = locationManager.getLastKnownLocation( LocationManager.NETWORK_PROVIDER );
return location;
} else if (gps_enabled) {
locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 5000, 10, this );
location = locationManager.getLastKnownLocation( LocationManager.GPS_PROVIDER );
return location;
}
}
}
} else {
Toast.makeText( NearMeActivity.this, R.string.isOnline, Toast.LENGTH_SHORT ).show();
}
return null;
}
/////////Sending LATLNG in json format to server///////////////
public void submitBtn(View view) {
if (isNetworkAvailable( this )) {
if (location != null) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
AsyncHttpClient client = new AsyncHttpClient();
RequestParams params = new RequestParams();
params.put( "latitude", latitude );
params.put( "longitude", longitude );
String uploadURL = "192.168.1.106";
client.post( uploadURL, params, new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
try {
String status = response.getString( "status" );
String msg = response.getString( "msg" );
//Toast.makeText( getApplicationContext(), status, Toast.LENGTH_SHORT ).show();
if (status.equals( "success" )) {
Toast.makeText( getApplicationContext(), msg, Toast.LENGTH_SHORT ).show();
} else if (status.equals( "failed" )) {
Toast.makeText( getApplicationContext(), msg, Toast.LENGTH_SHORT ).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
//Toast.makeText( getApplicationContext(), R.string.onFailure, Toast.LENGTH_SHORT ).show();
}
@Override
public void onStart() {
progressBar.setVisibility( View.VISIBLE );
System.out.println( "onStart" );
}
@Override
public void onFinish() {
progressBar.setVisibility( View.INVISIBLE );
System.out.println( "onFinish" );
}
} );
} else {
Toast.makeText( this, "Getting your current location...", Toast.LENGTH_SHORT ).show();
}
} else {
Toast.makeText( this, R.string.isOnline, Toast.LENGTH_SHORT ).show();
}
}
///////Check network connectivity///////////////////
private static boolean isNetworkAvailable(Context context) {
ConnectivityManager connectivity = (ConnectivityManager) context
.getSystemService( Context.CONNECTIVITY_SERVICE );
if (connectivity == null) {
Log.d( "NetworkCheck", "isNetworkAvailable: No" );
return false;
}
// get network info for all of the data interfaces (e.g. WiFi, 3G, LTE, etc.)
NetworkInfo[] info = connectivity.getAllNetworkInfo();
// make sure that there is at least one interface to test against
if (info != null) {
// iterate through the interfaces
for (NetworkInfo anInfo : info) {
// check this interface for a connected state
if (anInfo.getState() == NetworkInfo.State.CONNECTED) {
Log.d( "NetworkCheck", "isNetworkAvailable: Yes" );
return true;
}
}
}
return false;
}