R
R
Roman_Craft2020-04-19 19:14:05
Java
Roman_Craft, 2020-04-19 19:14:05

How to manage GET parameters of Retrofit requests?

Well, look.. I want to control the parameters of the get requests that I set in Link.java, but I do not know how to do this.
Code:

MainActivity.java:

import androidx.appcompat.app.AppCompatActivity;
 
import android.os.Bundle;
import android.widget.TextView;
 
import java.util.List;
 
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
 
public class MainActivity extends AppCompatActivity {
    TextView text;
    Link link;
    String url = "https://mp3go.dev/";
    Retrofit retrofit;
    String pesni = "";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text = findViewById(R.id.text);
        retrofit = new Retrofit.Builder().baseUrl(url).addConverterFactory(GsonConverterFactory.create()).build();
        link = retrofit.create(Link.class);
        link.getSongs().enqueue(new Callback<GetSongs>() {
            @Override
            public void onResponse(Call<GetSongs> call, Response<GetSongs> response) {
                List<Song> s = response.body().getResult();
                for(int i = 0;i<s.size();i++){
                    pesni += s.get(i).getTitle()+"\n";
                }
                text.setText(pesni);
            }
 
            @Override
            public void onFailure(Call<GetSongs> call, Throwable t) {
                text.setText(t.getMessage());
 
                t.printStackTrace();
            }
        });
    }
}


Link.java:

import retrofit2.Call;
import retrofit2.http.GET;
 
public interface Link {
@GET("api/tracks.searchq=Sting&search_type=&limit=50&offset=100&api_key=997Lsdfsd6iPciVDSsdffsdewR16rewzZYfoofpf1")
    Call<GetSongs> getSongs();
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
5
5am, 2020-04-19
​​@5am

the first link in google
https://square.github.io/retrofit/2.x/retrofit/ind...

Simple Example:


 @GET("/friends")
 Call<ResponseBody> friends(@Query("page") int page);
 
Calling with foo.friends(1) yields /friends?page=1.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question