首页 > 解决方案 > 如何在android文本视图上查看php脚本的结果

问题描述

我想使用改造获得一个 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);
?>

这是研究所服务

    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开发相当陌生,将不胜感激任何形式的帮助

标签: phpandroidretrofit2

解决方案


首先,对于服务器部分,您必须返回您需要在 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();
}

推荐阅读