try this
in Gradle
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.5.0'
in Interface eg: make RetrofitAPI.java
public static PostService postService = null;
public static PostService getService() {
if (postService == null) {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(interceptor).build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(base_url)
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build();
postService = retrofit.create(PostService.class);
}
return postService;
}
public interface API {
@FormUrlEncoded
@POST
Call<Post> createPost(@Url String url,
@Field("id_urzadzenia") String id_urzadzenia,
@Field("loginStr") String loginStr,
@Field("passwordStr") String passwordStr,
@Field("longitude") String longitude,
@Field("latitude") String latitude
);
}
in MainActivity
public void createPost(String loginStr, String passwordStr, String id_urzadzenia, double latitude, double longitude) throws IOException {
final Call<Post> call_api = RetrofitAPI.getService().createPost(url, id_urzadzenia, loginStr, passwordStr, longitude, latitude );
call_api.enqueue(new Callback<Post>() {
@Override
public void onResponse(Call<Post> call, Response<Post> response) {
if(response.body() != null){
// Toast.makeText(ACTIVITY.this, "Success!!!", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<Post> call, Throwable t) {
// Toast.makeText(ACTIVITY.this, "Error Occured", Toast.LENGTH_SHORT).show();
}
});
}
}
Change your php file like this. now it should insert into your database
//your database configuration
$con = mysqli_connect(DB_SERVER,DB_USER,DB_PASSWORD,DB_DATABASE);
$login = $_POST["loginStr"];
$id_urz = $_POST["id_urzadzenia"];
$dl_geo = $_POST["longitude"];
$sz_geo = $_POST["latitude"];
// echo "Login użytkownika: " .$login . "
";
// echo "ID urządzenia: " . $id_urz . "
";
// echo "Szerokość geograficzna: " . $sz_geo . "
";
// echo "Długość geograficzna: " . $dl_geo . "
";
date_default_timezone_get(Europe);
$godz = date("H:i:s");
$sql = "INSERT INTO bartek (id_urzadzenia,szerokosc,dlugosc,data) VALUES ('$id_urz','$sz_geo','$dl_geo','$godz')";
mysqli_query($con, $sql);
mysqli_close($con);
?>