怎样才能从PHP中获取结果?

huangapple go评论101阅读模式
英文:

How do I get the results from the php?

问题

以下是您要的翻译:

PHP 代码部分:

  1. 有数据库中的值,我正在创建一个搜索函数。
  2. 如果在搜索时没有结果,并且数组的长度为0,则我想在视图中显示 "Empty Result" 并将该值传递到 Android
  3. ```php
  4. <?php
  5. require_once 'conn.php';
  6. if(isset($_GET['name'])) {
  7. $name = $_GET['name'];
  8. $query = "SELECT `NAME`, AGE, `ADDRESS` FROM test WHERE `NAME` = '$name'";
  9. $result = mysqli_query($conn, $query);
  10. $response = array();
  11. while($row = mysqli_fetch_array($result)) {
  12. array_push(
  13. $response, array(
  14. 'name'=>$row['NAME'],
  15. 'age'=>$row['AGE'],
  16. 'address'=>$row['ADDRESS'])
  17. );
  18. }
  19. if(count($response) == 0) {
  20. $res = 'Empty Result';
  21. echo $res;
  22. } else {
  23. echo json_encode($response);
  24. }
  25. }
  26. mysqli_close($conn);
  27. ?>

Android 代码部分:

  1. public class MainActivity extends AppCompatActivity {
  2. SearchView searchView;
  3. ApiInterface apiInterface;
  4. TextView nameText, ageText, addressText;
  5. LinearLayout layout_search, layout_none_search;
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.activity_main);
  10. nameText = findViewById(R.id.nameText);
  11. ageText = findViewById(R.id.ageText);
  12. addressText = findViewById(R.id.addressText);
  13. layout_search = findViewById(R.id.layout_search);
  14. layout_none_search = findViewById(R.id.layout_none_search);
  15. searchView = findViewById(R.id.searchView);
  16. searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
  17. @Override
  18. public boolean onQueryTextSubmit(String s) {
  19. personList(s);
  20. return false;
  21. }
  22. @Override
  23. public boolean onQueryTextChange(String s) {
  24. return false;
  25. }
  26. });
  27. }
  28. public void personList(String key) {
  29. apiInterface = ApiClient.getApiClient().create(ApiInterface.class);
  30. Call<List<Person>> call = apiInterface.getPerson(key);
  31. call.enqueue(new Callback<List<Person>>() {
  32. @Override
  33. public void onResponse(Call<List<Person>> call, Response<List<Person>> response) {
  34. if(!response.body().isEmpty()) {
  35. layout_search.setVisibility(View.VISIBLE);
  36. layout_none_search.setVisibility(View.GONE);
  37. Person person = response.body().get(0);
  38. nameText.setText(person.getName());
  39. ageText.setText(String.valueOf(person.getAge()));
  40. addressText.setText(person.getAddress());
  41. } else if(response.body().equals("Empty Result")) {
  42. layout_search.setVisibility(View.GONE);
  43. layout_none_search.setVisibility(View.VISIBLE);
  44. }
  45. }
  46. @Override
  47. public void onFailure(Call<List<Person>> call, Throwable t) {
  48. Log.e("onFailure", t.toString());
  49. }
  50. });
  51. }
  52. }

如果我现在运行代码,会弹出 "com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $"。如何在 Android 中获取 "Empty Result"?

  1. ++ 添加我的 POJO
  2. ```java
  3. public class Person {
  4. @SerializedName("name") private String name;
  5. @SerializedName("age") private int age;
  6. @SerializedName("address") private String address;
  7. public String getName() {
  8. return name;
  9. }
  10. public void setName(String name) {
  11. this.name = name;
  12. }
  13. public int getAge() {
  14. return age;
  15. }
  16. public void setAge(int age) {
  17. this.age = age;
  18. }
  19. public String getAddress() {
  20. return address;
  21. }
  22. public void setAddress(String address) {
  23. this.address = address;
  24. }
  25. }
英文:

There are values in DB, and I'm creating a search function.

  1. &lt;?php
  2. require_once &#39;conn.php&#39;;
  3. if(isset($_GET[&#39;name&#39;])) {
  4. $name = $_GET[&#39;name&#39;];
  5. $query = &quot;SELECT `NAME`, AGE, `ADDRESS` FROM test WHERE `NAME` = &#39;$name&#39;&quot;;
  6. $result = mysqli_query($conn, $query);
  7. $response = array();
  8. while($row = mysqli_fetch_array($result)) {
  9. array_push(
  10. $response, array(
  11. &#39;name&#39;=&gt;$row[&#39;NAME&#39;],
  12. &#39;age&#39;=&gt;$row[&#39;AGE&#39;],
  13. &#39;address&#39;=&gt;$row[&#39;ADDRESS&#39;])
  14. );
  15. }
  16. if(count($response) == 0) {
  17. $res = &#39;Empty Result&#39;;
  18. echo $res;
  19. } else {
  20. echo json_encode($response);
  21. }
  22. }
  23. mysqli_close($conn);
  24. ?&gt;

If there is no result when I search, and the length of the array is 0, I want to show the Empty Result in View and bring the value to android.

And Android wants to float layout_none_search if the imported value is Empty Result.

  1. public class MainActivity extends AppCompatActivity {
  2. SearchView searchView;
  3. ApiInterface apiInterface;
  4. TextView nameText, ageText, addressText;
  5. LinearLayout layout_search, layout_none_search;
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.activity_main);
  10. nameText = findViewById(R.id.nameText);
  11. ageText = findViewById(R.id.ageText);
  12. addressText = findViewById(R.id.addressText);
  13. layout_search = findViewById(R.id.layout_search);
  14. layout_none_search = findViewById(R.id.layout_none_search);
  15. searchView = findViewById(R.id.searchView);
  16. searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
  17. @Override
  18. public boolean onQueryTextSubmit(String s) {
  19. personList(s);
  20. return false;
  21. }
  22. @Override
  23. public boolean onQueryTextChange(String s) {
  24. return false;
  25. }
  26. });
  27. }
  28. public void personList(String key) {
  29. apiInterface = ApiClient.getApiClient().create(ApiInterface.class);
  30. Call&lt;List&lt;Person&gt;&gt; call = apiInterface.getPerson(key);
  31. call.enqueue(new Callback&lt;List&lt;Person&gt;&gt;() {
  32. @Override
  33. public void onResponse(Call&lt;List&lt;Person&gt;&gt; call, Response&lt;List&lt;Person&gt;&gt; response) {
  34. if(!response.body().isEmpty()) {
  35. layout_search.setVisibility(View.VISIBLE);
  36. layout_none_search.setVisibility(View.GONE);
  37. Person person = response.body().get(0);
  38. nameText.setText(person.getName());
  39. ageText.setText(String.valueOf(person.getAge()));
  40. addressText.setText(person.getAddress());
  41. } else if(response.body().equals(&quot;Empty Result&quot;)) {
  42. layout_search.setVisibility(View.GONE);
  43. layout_none_search.setVisibility(View.VISIBLE);
  44. }
  45. }
  46. @Override
  47. public void onFailure(Call&lt;List&lt;Person&gt;&gt; call, Throwable t) {
  48. Log.e(&quot;onFailure&quot;, t.toString());
  49. }
  50. });
  51. }
  52. }

But if I run the code now, com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $ pops up.

How can I get the Empty Result in Android?

++ Add My POJO

  1. public class Person {
  2. @SerializedName(&quot;name&quot;) private String name;
  3. @SerializedName(&quot;age&quot;) private int age;
  4. @SerializedName(&quot;address&quot;) private String address;
  5. public String getName() {
  6. return name;
  7. }
  8. public void setName(String name) {
  9. this.name = name;
  10. }
  11. public int getAge() {
  12. return age;
  13. }
  14. public void setAge(int age) {
  15. this.age = age;
  16. }
  17. public String getAddress() {
  18. return address;
  19. }
  20. public void setAddress(String address) {
  21. this.address = address;
  22. }
  23. }

答案1

得分: 0

因为这个值无法被解析为 JSON。
你可以使用统一的 JSON 返回格式。

  1. if(count($response) == 0) {
  2. echo json_encode([
  3. "result" => "Empty Result",
  4. "data" => $response
  5. ]);
  6. } else {
  7. echo json_encode([
  8. "result" => "Has rows",
  9. "data" => $response
  10. ]);
  11. }

在你的 Android 代码中,以 JSON 格式解析 response.body()

英文:

Its because the value can't be parsed as JSON.
You can use uniform return format of json.

  1. if(count($response) == 0) {
  2. echo json_encode([
  3. &quot;result&quot; =&gt; &quot;Empty Result&quot;,
  4. &quot;data&quot; =&gt; $response
  5. ]);
  6. } else {
  7. echo json_encode([
  8. &quot;result&quot; =&gt; &quot;Has rows&quot;,
  9. &quot;data&quot; =&gt; $response
  10. ]);
  11. }

and in your android parse response.body() in JSON format.

huangapple
  • 本文由 发表于 2020年9月15日 16:25:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63897941.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定