如何在android文本视图上查看php脚本的结果

时间:2018-09-18 12:22:29

标签: php android retrofit2

我想通过改型获得PHP脚本的结果以显示在Android Textview上,我能够将编辑文本中的数据发送到数据库中,而执行类似的操作

这是用于post方法

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();


                        }

get方法的代码

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();


                                }

我只能发送进行查询所需的字段,但我不知道如何显示结果。

这是我正在使用的get方法的PHP脚本

<?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);
?>

这是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);


}

对于android开发来说还算是新手,将不胜感激

1 个答案:

答案 0 :(得分:1)

首先,对于服务器部分,您必须返回所有需要在Android中显示的字段:

$result = mysqli_query($conn, $sql);
if ($result) {
    $row = mysqli_fetch_assoc($result);
    echo(json_encode(array(
      'Message' => "OK",
      'Status' => 1,
      'Surname' => $row['surname'], 
      'Firstname' => $row['firstname'], 
      'Othername' => $row['othername']
      // Rest of the fields
    )));
} else {
    echo(json_encode(array('Message' => mysql_error($conn), 'Status' => 0)));
}

对于客户端:

ServiceResponse rr = response.body();
if (rr.getStatus().contentEquals("1"))
{
    Toast.makeText(MainActivity.this, "Successfully", Toast.LENGTH_LONG).show();
    txtSurname.setText(rr.getSurname());
    txtFirstname.setText(rr.getFirstname());
    // Rest of the fields
}
else
{
    Toast.makeText(MainActivity.this,"Error", Toast.LENGTH_LONG).show();
}