Я хочу изучить модернизацию, но не могу решить эту ошибку
мое приложение должно получить gson из php-кода, но у меня ошибка при запуске ожидаемый begin_array, но была строка
мой код
package com.example.rami_.retrofitapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.util.List;
import okhttp3.OkHttpClient;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class MainActivity extends AppCompatActivity {
TextView nametxt, agetxt, phonetxt, emailtxt;
Button retrieveBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nametxt = (TextView) findViewById(R.id.nametxt);
agetxt = (TextView) findViewById(R.id.agetxt);
phonetxt = (TextView) findViewById(R.id.phonetxt);
emailtxt = (TextView) findViewById(R.id.emailtxt);
retrieveBtn = (Button) findViewById(R.id.retrieveBtn);
retrieveBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fetchData();
}
});
}
private void fetchData() {
OkHttpClient client = new OkHttpClient();
Gson gson = new GsonBuilder()
.setLenient()
.create();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Api.BASE_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
Api api = retrofit.create(Api.class);
Call<List<Details_Pojo>> call = api.getstatus();
call.enqueue(new Callback<List<Details_Pojo>>() {
@Override
public void onResponse(Call<List<Details_Pojo>> call, Response<List<Details_Pojo>> response) {
List<Details_Pojo> adslist = response.body();
String name = adslist.get(0).getName();
String age = adslist.get(0).getAge();
String phone = adslist.get(0).getPhone();
String email = adslist.get(0).getEmail();
nametxt.setText(name);
agetxt.setText(age);
phonetxt.setText(phone);
emailtxt.setText(email);
}
@Override
public void onFailure(Call<List<Details_Pojo>> call, Throwable t) {
Toast.makeText(MainActivity.this, ""+t.getMessage().toString(), Toast.LENGTH_SHORT).show();
}
});
}
}
модель класса
package com.example.rami_.retrofitapp;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
/**
* Created by Android on 1/6/2018.
*/
public class Details_Pojo {
@SerializedName("Name")
private String Name;
@SerializedName("Age")
private String Age;
@SerializedName("Phone")
private String Phone;
@SerializedName("Email")
private String Email;
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getAge() {
return Age;
}
public void setAge(String age) {
Age = age;
}
public String getPhone() {
return Phone;
}
public void setPhone(String phone) {
Phone = phone;
}
public String getEmail() {
return Email;
}
public void setEmail(String email) {
Email = email;
}
}
api >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
package com.example.rami_.retrofitapp;
import java.util.List;
import retrofit2.Call;
import retrofit2.http.GET;
/**
* Created by Android on 1/6/2018.
*/
public interface Api {
String BASE_URL = "http://10.0.2.2/retrofitapp/";
@GET("fetch_data.php")
Call<List<Details_Pojo>> getstatus();
}
php-код, это связано с базой данных, но здесь мы не получаем никаких данных из базы данных, мы используем пока только ответы
<?php
require "init.php";
$name = $_GET["Name"];
$age = $_GET["Age"];
$phone = $_GET["Phone"];
$email = $_GET["Email"];
$sql = "select * from login_info where name ='$name'";
$result = mysqli_query($con, $sql);
if(mysqli_num_rows($result) > 0)
{
$status ="exist";
}
else
{
$sql = "insert into login_info(name, age, phone, email) VALUES ('$name', '$age', '$phone',$email)";
if (mysqli_query($con, $sql))
{
$status ="ok";
}
else
{
$status ="error";
}
}
$response = array('Name' => "Rami",'Age' => "24",'Phone' =>
"01004562638",'Email' => "24");
echo json_encode($response);
mysqli_close($con);
?>