繁体   English   中英

我如何从 php 获取结果?

[英]How do I get the results from the php?

数据库中有值,我正在创建一个搜索功能。

<?php

require_once 'conn.php';

if(isset($_GET['name'])) {
    $name = $_GET['name'];
    $query = "SELECT `NAME`, AGE, `ADDRESS` FROM test WHERE `NAME` = '$name'";
    $result = mysqli_query($conn, $query);

    $response = array();
    while($row = mysqli_fetch_array($result)) {
        array_push(
            $response, array(
                'name'=>$row['NAME'],
                'age'=>$row['AGE'],
                'address'=>$row['ADDRESS'])
            );
    }

    if(count($response) == 0) {
        $res = 'Empty Result';
        echo $res;
    } else {
        echo json_encode($response);
    }
}

mysqli_close($conn);

?>

如果我搜索时没有结果,并且数组的长度为0,我想在View中显示Empty Result并将值带到android。

如果导入的值为Empty Result ,Android 想要浮动 layout_none_search 。

public class MainActivity extends AppCompatActivity {

    SearchView searchView;
    ApiInterface apiInterface;
    TextView nameText, ageText, addressText;

    LinearLayout layout_search, layout_none_search;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        nameText = findViewById(R.id.nameText);
        ageText = findViewById(R.id.ageText);
        addressText = findViewById(R.id.addressText);

        layout_search = findViewById(R.id.layout_search);
        layout_none_search = findViewById(R.id.layout_none_search);

        searchView = findViewById(R.id.searchView);
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String s) {
                personList(s);

                return false;
            }

            @Override
            public boolean onQueryTextChange(String s) {
                return false;
            }
        });
    }

    public void personList(String key) {
        apiInterface = ApiClient.getApiClient().create(ApiInterface.class);
        Call<List<Person>> call = apiInterface.getPerson(key);
        call.enqueue(new Callback<List<Person>>() {
            @Override
            public void onResponse(Call<List<Person>> call, Response<List<Person>> response) {
                if(!response.body().isEmpty()) {
                    layout_search.setVisibility(View.VISIBLE);
                    layout_none_search.setVisibility(View.GONE);

                    Person person = response.body().get(0);
                    nameText.setText(person.getName());
                    ageText.setText(String.valueOf(person.getAge()));
                    addressText.setText(person.getAddress());
                } else if(response.body().equals("Empty Result")) {
                    layout_search.setVisibility(View.GONE);
                    layout_none_search.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onFailure(Call<List<Person>> call, Throwable t) {
                Log.e("onFailure", t.toString());
            }
        });
    }
}

但是如果我现在运行代码, com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $弹出。

如何在 Android 中获得Empty Result

++ 添加我的 POJO

public class Person {
    @SerializedName("name") private String name;
    @SerializedName("age") private int age;
    @SerializedName("address") private String address;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

这是因为该值无法解析为 JSON。 您可以使用 json 的统一返回格式。

if(count($response) == 0) {

    echo json_encode([
           "result" => "Empty Result",
            "data" => $response
         ]);
} else {
    echo json_encode([
           "result" => "Has rows",
            "data" => $response
         ]);
}

并在您的 android 中以 JSON 格式解析response.body()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM