Interface used within Android Studio.
public interface ApiInterface {
@GET("test.php")
Call<Person> getPerson(@Query("name") String keyword);
}
Call<List<Person>>
Not like that, use it like that
public void personList(String key) {
apiInterface = ApiClient.getApiClient().create(ApiInterface.class);
Call<Person> call = apiInterface.getPerson(key);
call.enqueue(new Callback<Person>() {
@Override
public void onResponse(Call<Person> call, Response<Person> response) {
Person person = response.body();
nameText.setText(person.getName());
}
@Override
public void onFailure(Call<Person> call, Throwable t) {
Log.e("onFailure", t.toString());
}
});
}
I'd like to call and use it in the main activity.
The current php statement looks like this.
<?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_assoc($result)) {
array_push(
$response, array(
'name'=>$row['NAME'],
'age'=>$row['AGE'],
'address'=>$row['ADDRESS'])
);
}
echo json_encode($response);
}
mysqli_close($conn);
?>
Upload and run on the web server
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line
I thought it was an object, but JsonSyntaxException appears because it is an array.
Call I don't think I should send php statements in an array form from the beginning to receive it like this, so how do I modify PHP statements? I knew why there was an error, but I didn't study Php deeply, so I don't know Php yet.
android retrofit2
Hi, how are you?
As you know, PHP should encode the result value in the form of Json Object instead of Array.
I don't think I can see the encoding on the code!
echo json_encode($response);
// It just outputs what json_code($response) is.
© 2024 OneMinuteCode. All rights reserved.