Android retrofit data passing with PHP and Mysql
Retrofit is an library for android which is REST API Client for android.
so which can be used to connect your application with online database or you can connect with your local machine Mysql database and this library also help developers to develop application based on API.
1. first we need to have a REST API client for that we using PHP and Mysql..!
we need to create a database in phpmyadmin panel click create new where you need to enter database name and click submit then click you your newly created database then add table then add a table and add columns. for example i am creating a database and added a users table which has username and password column and add some data.
2. we need to create a PHP page to check that username and password is already registered and by that we need to create a response with PHP.
<?php
error_reporting(0);
if ($_SERVER[‘REQUEST_METHOD’] == ‘GET’) {
include(“../connect/init.php”);
if (isset($_GET[‘username’]) && isset($_GET[‘password’])){
// username and password sent from link
$myusername = $_GET[‘username’];
$mypassword = $_GET[‘password’];
$query = “SELECT * FROM users WHERE username = ‘$myusername’ and password = ‘$mypassword’”;
$result = mysqli_query($connection, $query);
$data = mysqli_num_rows($result);
if($data>0) {
$error=”ok”;
echo json_encode(array(“response”=>$error,”name”=>$myusername));
}else {
$error = “failed”;
echo json_encode(array(“response”=>$error));
}
}
}
mysqli_close($connection);
?>
for the above code we created a response with json_encode( ) and that return data as ok and also it might return error when no user found.
save this php file in server and then pass data to this file by php get method.
3. Now a return response is created with the server so then we need create a android project and then we need to add retrofit library to gradle file
implementation 'com.squareup.retrofit2:retrofit:2.0.2'implementation 'com.squareup.retrofit2:converter-gson:2.0.2'and then you need to create three files in android two java class and one interface,create a java class file for apiclient.javapackage com.example.demo;import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;public class ApiClient { public static final String BASE_URL="https://yoururl/android/"; public static Retrofit retrofit = null; public static Retrofit getApiClient(){
if(retrofit==null){
retrofit =new Retrofit.Builder().baseUrl(BASE_URL).addConverterFactory(GsonConverterFactory.create()).build();
}
return retrofit;
}
}
then create a interface file ApiInterface.java then create a class and then following code:
package com.example.demo;import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Query;public interface ApiInterface {
@GET("login.php")
Call<User> performLogin(@Query("username") String user, @Query("password") String pass);
}then we need to create another java class file for model the data that we get from response.file name User.java and then add the following code:import com.google.gson.annotations.SerializedName;public class User {
@SerializedName("response")
private String Response; public String getResponse() {
return Response;
}}4. In the main activity add the following method:above Oncreate method public static ApiInterface apiInterface;and this within oncreate method apiInterface = ApiClient.getApiClient().create(ApiInterface.class);
and this outside oncreate methodprivate void performLogin(){
String username,password;
username=usrname.getText().toString();
password=pwd.getText().toString();
Call<User> call =login.apiInterface.performLogin(username,password);
call.enqueue(new Callback<User>() {
@Override public void onResponse(Call<User> call, Response<User> response) {
if(response.body().getResponse().equals("ok")){
// add your code to get data
}
else if(response.body().getResponse().equals("failed")){
display error message
}
}
@Override public void onFailure(Call<User> call, Throwable t) { //display errror message
}
});
usrname.setText("");
pwd.setText("");
}Thats it now you might understand retrofit...this is my first post so there maybe some mistake that i will change in future. so,if you have any doubts then comment below...! thank you....!