0

I am developing with retrofit, but I have a problem. I want to get the results from my server.

This is my result json from my script php:

[{"rubrique_id":"1","rubrique_lib":"syst\u00e8me "},
 {"rubrique_id":"2","rubrique_lib":"s\u00e9curit\u00e9"},
 {"rubrique_id":"3","rubrique_lib":"information"}]

This is my model class in java:

public class Bureau {

    @SerializedName("rubrique_id")
    private String rubrique_id;

    @SerializedName("rubrique_lib")
    private String rubrique_lib;

    public String getRubrique_id() {
        return rubrique_id;
    }

    public void setRubrique_id(String rubrique_id) {
        this.rubrique_id = rubrique_id;
    }

    public String getRubrique_lib() {
        return rubrique_lib;
    }

    public void setRubrique_lib(String rubrique_lib) {
        this.rubrique_lib = rubrique_lib;
    }


}

This is my API class:

public interface BureauApi {

    @GET("/")
    Call<List<Bureau>> getBureau();
}

This is main class where I implemented the retrofit library:

public class ParametreActivity extends AppCompatActivity {
    private Spinner spinner = null;
    private static final String BASE_URL = "http://192.168.56.1:80/posteboosterAPI/methodes.php/";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_parametre);
        spinner = (Spinner)findViewById(R.id.list_bureau);
        final List<String> listBureau = new ArrayList<String>();
        listBureau.add("bureau1");
        listBureau.add("bureau2");

        Gson gson = new GsonBuilder()
                .setLenient()
                .create();

        Retrofit retrofit = new Retrofit.Builder()
                             .baseUrl(BASE_URL)
                             .addConverterFactory(GsonConverterFactory.create(gson))
                             .build();
        BureauApi  service = retrofit.create(BureauApi.class);

        Call<List<Bureau>> call = service.getBureau();
         call.enqueue(new Callback<List<Bureau>>() {
             @Override
             public void onResponse(Call<List<Bureau>> call, Response<List<Bureau>> response) {

                 if (response.isSuccessful()) {

                     for (Bureau bureaus :response.body()
                             ) {
                         Log.i("log", bureaus.getRubrique_lib());
                     }
                 }else {

                     Log.i("log", "l'objet est vide "+response.message());
                 }
             }

             @Override
             public void onFailure(Call<List<Bureau>> call, Throwable t) {
                 Log.i("log","erreur survenu dans la requete "+ t.getMessage());

             }
         });
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,listBureau);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
    }
}

And the console is giving me the following error:

Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $

1 Answer 1

1

Try this:

public class BureauCollection
{
   List<Bureau> bureauList;
}

and

Call<BureauCollection> getBureau();

Change BASE URL: "http://192.168.56.1:80/" and in API: @GET("/posteboosterAPI/methodes.php")

Sign up to request clarification or add additional context in comments.

4 Comments

I have tried this but i had " EXPECTED BEGIN OBJECT BUT WAS STRING " as error.thank yu
@Rushi M Thakker when i apply your example , l have this error :
Java.lang.illegalstateexception: Expected begin_object but was begin_array
ok. so what exactly u did? could you please post an update to your code or answer your own question? @JoelAkoupo

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.