我如何从php获取结果?

时间:2020-09-15 08:25:57

标签: java php android

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

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

1 个答案:

答案 0 :(得分:0)

这是因为无法将值解析为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()