douxi3085 2018-09-18 12:22
浏览 114
已采纳

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

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

  • 写回答

1条回答 默认 最新

  • duanben1909 2018-09-19 07:14
    关注

    First, for the server part, you have to return all the fields that you need to show in 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)));
    }
    

    And for the client side:

    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();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 前后端分离的学习疑问?
  • ¥15 stata实证代码答疑
  • ¥15 MATLAB数据处理插值
  • ¥50 husky+jaco2实现在gazebo与rviz中联合仿真
  • ¥15 dpabi预处理报错:Error using y_ExtractROISignal (line 251)
  • ¥15 在虚拟机中配置flume,无法将slave1节点的文件采集到master节点中
  • ¥15 husky+kinova jaco2 仿真
  • ¥15 zigbee终端设备入网失败
  • ¥15 金融监管系统怎么对7+4机构进行监管的
  • ¥15 硬件IIC从模式的数据发送,中断数据的接收,不能用HAL库(按照时序图)