Cap
Cap

Reputation: 57

How to pass Variables into GET-Parameters in Retrofit

I am new in java and Android. And I am trying to pass some Variable(s) into a Retrofit GET-Call. But nothing works for me, so pleas want you take a look at my code?

What do I have to change to send my Variables:

to the Server?

My java-file:


    public class Orte extends AppCompatActivity {

...

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_orte);

        Intent myIntent = getIntent(); // gets the previously created intent
        myActuelFullName = myIntent.getStringExtra("paramFullName"); // will return "paramFullName"

        getEntries(myActuelFullName);
    }


    private void getEntries(String myActuelFullName) {

                Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://myDomain.de/api/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        JsonPlaceHolderApi jsonPlaceHolderApi = retrofit.create(JsonPlaceHolderApi.class);

        String myOtherInfo = "testing123";

        Call<List<Entries>> call = jsonPlaceHolderApi.getEntries();
        Log.d("DEBUG", myActuelFullName ); // at this point the Variable is known!

        call.enqueue(new Callback<List<Entries>>() {
            @Override
            public void onResponse(Call<List<Entries>> call, Response<List<Entries>> response) {
                if (!response.isSuccessful()) {
                    //textViewResult.setText("Code: " + response.code());
                    return;
                }
//


            @Override
            public void onFailure(Call<List<Entries>> call, Throwable t) {
                //  textViewResult.setText(t.getMessage());
            }
        });

And my Placeholder-Api:

public interface JsonPlaceHolderApi {


    @GET("get_entries.php")
    Call<List<Entries>> getEntries();

    @POST("http://myDomain.de/api/mypost.php/")
    Call<Post> createPost(@Body Post post);

}

Upvotes: 0

Views: 840

Answers (1)

Antonio
Antonio

Reputation: 1274

You have to first add the query parameters in API interface class. After adding the two query parameters, your class should look like:

public interface JsonPlaceHolderApi {


    @GET("get_entries.php")
    Call<List<Entries>> getEntries(@Query("myActuelFullName") String myActuelFullName, @Query("myOtherInfo") String myOtherInfo);

    @POST("http://myDomain.de/api/mypost.php/")
    Call<Post> createPost(@Body Post post);

}

and in your activity, the function call should be like:

Call<List<Entries>> call = jsonPlaceHolderApi.getEntries(myActuelFullName,myOtherInfo);

Upvotes: 1

Related Questions