1. Add given below dependencies in your in build.gradle(Module:app)
implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
2. Add Internet Permission in your Manifest file.
<uses-permission android:name="android.permission.INTERNET" />
3. activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="@+id/etfname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20sp"
android:hint="Firstname"
android:textSize="20sp"
android:textStyle="bold" />
<EditText
android:id="@+id/etlname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20sp"
android:hint="Lastname"
android:textSize="20sp"
android:textStyle="bold" />
<Button
android:id="@+id/btnsave"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20sp"
android:text="Save"
android:textSize="20sp" />
</LinearLayout>
4. connection.php
$host="localhost";
$user="YourUser";
$passwd="YourPassword";
$db="YourDatabaseName";
$con=mysqli_connect("localhost","YourUser","YourPassword","YourDatabaseName") or die("Disconnect");
?>
5. insert_user.php
include('connection.php');
$columnname=$_REQUEST['columnname'];
$columnname=$_REQUEST['columnname'];
$sql="insert into table_name(columnname,columnname) values ('$columnname','$columnname')";
if(mysqli_query($con,$sql))
{
echo json_encode(array('response'=>"Successfully added"));
}
else
{
echo json_encode(array('response'=>"Failed"));
}
mysqli_close($con);
?>
6. Test your link into POSTMAN
POSTMAN DOWNLOAD LINK : https://www.getpostman.com/downloads/
7. Create a new java Class
ApiClient.java
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class ApiClient {
public static final String BASE_URL = "Your Project Main URL";
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;
}
}
8. Create another java class
Details.java
public class Details {
String firstname, lastname, response;
public String getResponse() {
return response;
}
}
9. Create a new Interface
ApiInterface.java
import retrofit2.Call;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.POST;
public interface ApiInterface {
@FormUrlEncoded
@POST("YourWebServiceName.php")
Call<Details> addData(@Field("Columnname") String firstname, @Field("Columnname") String lastname);
}
10. MainActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class MainActivity extends AppCompatActivity {
EditText etfname, etlname;
Button btnsave;
ApiInterface apiInterface;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etfname = findViewById(R.id.etfname);
etlname = findViewById(R.id.etlname);
btnsave = findViewById(R.id.btnsave);
btnsave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
apiInterface = ApiClient.getApiClient().create(ApiInterface.class);
Call<Details> call = apiInterface.addData(etfname.getText().toString(), etlname.getText().toString());
call.enqueue(new Callback<Details>() {
@Override
public void onResponse(Call<Details> call, Response<Details> response) {
Details details = response.body();
Toast.makeText(MainActivity.this, "" + details.getResponse(), Toast.LENGTH_SHORT).show();
etfname.getText().clear();
etlname.getText().clear();
}
@Override
public void onFailure(Call<Details> call, Throwable t) {
}
});
}
});
}
}
Click here to download source code |
Thanks for tutorial.
ReplyDelete