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

报告相同问题?

悬赏问题

  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法