Skip to content

Instantly share code, notes, and snippets.

@bioyeneye
Forked from mberberoglu/retrofit-android-1
Created October 29, 2018 05:17
Show Gist options
  • Save bioyeneye/2a709da3964afb8f095e42125af19ad5 to your computer and use it in GitHub Desktop.
Save bioyeneye/2a709da3964afb8f095e42125af19ad5 to your computer and use it in GitHub Desktop.
Retrofit Android
//Interface tanımlama
public interface UserService {
@GET("/users/{username}")
UserModel getUser(@Path("username") String name);
}
//Adapter ve servis oluşturma
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint("https://api.mustafab.net")
.build();
UserService service = restAdapter.create(UserService.class);
//İstek Çağırma
UserModel user = service.getUser("mustafab");
//Interface tanımlama
public interface UserService {
@GET("/users/{username}")
void getUser(@Path("username") String name, Callback<UserModel> callback);
}
//Adapter ve servis oluşturma
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint("https://api.mustafab.net")
.build();
UserService service = restAdapter.create(UserService.class);
//İstek Çağırma
Callback<UserModel> callback = new Callback<UserModel>() {
@Override
public void success(UserModel user, Response response) {
}
@Override
public void failure(RetrofitError retrofitError) {
}
};
service.getUser("mustafab", callback);
// http://api.mustafab.net/posts adresine aşağıdaki parametreler ile POST isteği
public interface PostService {
@FormUrlEncoded
@POST("/posts")
void get(
@Field("token") String token,
@Field("placeId") String placeId,
@Field("rate") int rate,
@Field("text") String text,
@Field("photo") String photo,
Callback<PostReturnModel> callback
);
}
// username => mustafab, limit => 10, page => 2
// http://api.mustafab.net/users/mustafab/posts?limit=10&page=2 adresine GET isteği
public interface UserService {
@GET("/users/{username}/posts")
void getUser(
@Path("username") String name,
@Query("limit") String limit,
@Query("page") String page,
@Query("order") String order,
Callback<UserPostModel> callback
);
}
//Custom Headerlar
public interface UserService {
@Headers({
"Accept: application/xml",
"User-Agent: MustafaB-Retrofit-App"
})
@GET("/users/{username}")
void getUser(@Path("username") String name, Callback<UserModel> callback);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment