-1

i want learn retrofit but i cannot solve this error my app must get gson from php code but i have error when run expected begin_array but was string my code

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();

            }
        });
    }
}

model class

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 code this connect with data base but here we dose not get any data from data base only we use respoans for now

<?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);
?>
Bhavesh Nayi
  • 3,626
  • 1
  • 27
  • 42

1 Answers1

0

Please post your JSON response so we can help more. This error happens when your code that parses your API is expecting a list in a JSON Array but instead Retrofit found a String which causes an exception that stops Retrofit from completing the parsing process.

In other words, you're expecting this as a response:

{
  [
    { .. }
  ]
}

But instead you received this:

{
  "SOME STRING"
}

So you have two options to fix this, either you change your API to return a list to match the format you are expecting or you change your Retrofit response object to match what is actually received from the API which is a String.

eyadMhanna
  • 2,412
  • 3
  • 31
  • 49