I want to get the result of a PHP script to display on Android Textview using retrofit I was able to send data in an edit text to the database doing something like this
This is for the post method
InstituteService instituteService = ApiClient.getApiClient(getApplicationContext()).create(InstituteService.class);
Call<ServiceResponse> calRegStudent = instituteService.UploadStudentsRecord(Surname,Firstname,Othername,Address,Age,Sex,Parentemail,Sclass,Regno,Tagno,Rollcallno,L_Finger1,L_Finger2,R_Finger1,R_Finger2,District_code,Zone_code,School_code);
calRegStudent.enqueue(new Callback<ServiceResponse>() {
@Override
public void onResponse(Call<ServiceResponse> call, Response<ServiceResponse> response) {
if(response.body() != null)
{
ServiceResponse rr= response.body();
if(rr.getStatus().contentEquals("1"))
{
Toast.makeText(MainActivity.this,"Data Submit Successfully", Toast.LENGTH_LONG).show();
ClearEditTextAfterDoneTask();
}
else
{
Toast.makeText(MainActivity.this,"Error", Toast.LENGTH_LONG).show();
}
The code for the get Method
InstituteService instituteService = ApiClient.getApiClient(getApplicationContext()).create(InstituteService.class);
Call<ServiceResponse> calRegStudent = instituteService.StudentGetRecord(Parentemail);
calRegStudent.enqueue(new Callback<ServiceResponse>() {
@Override
public void onResponse(Call<ServiceResponse> call, Response<ServiceResponse> response) {
if(response.body() != null)
{
ServiceResponse rr= response.body();
if(rr.getStatus().contentEquals("1"))
{
Toast.makeText(MainActivity.this,"Successfully", Toast.LENGTH_LONG).show();
ClearEditTextAfterDoneTask();
}
else
{
Toast.makeText(MainActivity.this,"Error", Toast.LENGTH_LONG).show();
}
I can only send the field needed to make the query but I have no idea how to display the result.
This the PHP script of the get method I am working on
<?php
//Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
//Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$parentemail=htmlspecialchars( $_GET["parentemail"]);
$sql = "SELECT surname,firstname,othername,rollcallno FROM studentTable WHERE parentemail='$parentemail';";
$result = mysqli_query($conn, $sql);
if (mysqli_query($conn, $sql)) {
echo(json_encode(array('Message'=>"New record created successfully",'Status'=>1)));
//echo "New record created successfully";
} else {
echo(json_encode(array('Message'=>mysql_error($conn),'Status'=>0)));
//echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
This is the InstituteService
package com.ainakay.studentapp;
import java.util.List;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.GET;
import retrofit2.http.Header;
import retrofit2.http.POST;
public interface InstituteService {
@FormUrlEncoded
@POST("insertin.php")
Call<ServiceResponse> UploadStudentsRecord(@Field("surname") String surname,@Field("firstname") String firstname,
@Field("othername") String othername,@Field("address") String address,
@Field("age") String age,@Field("sex") String sex,@Field("parentemail") String parentemail,
@Field("sclass") String sclass,@Field("regno") String regno,
@Field("tagno") String tagno,@Field("rollcallno") String rollcallno,
@Field("l_Finger1") String l_Finger1,@Field("l_Finger2") String l_Finger2,
@Field("r_Finger1") String r_Finger1,@Field("r_Finger2") String r_Finger2,
@Field("district_code") String district_code,@Field("zone_code") String zone_code,@Field("school_code") String school_code);
@FormUrlEncoded
@GET("parentgetstudent.php")
Call<ServiceResponse> StudentGetRecord(@Field("parentemail")String parentemail);
}
Please am fairly new to android development will appreciate any sort of help