Design pattern for retrofit interface
Tag : java , By : Fenix Drakken
Date : March 29 2020, 07:55 AM
should help you out I have trouble with the design of my Retrofit interface creator. I want to be able to instanciate the API interface in a generic way and update the corresponding instance whenever a token is passed. Currently, when I update the token, I have to call createService() method again to get the new instance that used the token in the generation of the Interface... , Make your interceptor look like this: public class TokenInterceptor implements Interceptor {
private String token;
public String getToken() {
return token;
}
@Override
public Response intercept(Chain chain) throws IOException {
Request original = chain.request();
// Request customization: add request headers
Request.Builder requestBuilder = original.newBuilder()
.header("Cache-Control", "no-cache")
.header("Accept", "application/json")
.method(original.method(), original.body());
if (getToken() != null) {
requestBuilder.header("Authorization", "Bearer " + AmzToken);
}
Request request = requestBuilder.build();
return chain.proceed(request);
}
public void setToken(String token) {
this.token = token;
}
}
|
Android Retrofit Design Patterns
Tag : java , By : user186435
Date : March 29 2020, 07:55 AM
I wish this helpful for you I usually use singleton pattern with following structure : first define ServiceHelper like following : public class ServiceHelper {
private static final String ENDPOINT = "http://test.com";
private static OkHttpClient httpClient = new OkHttpClient();
private static ServiceHelper instance = new ServiceHelper();
private IPlusService service;
private ServiceHelper() {
Retrofit retrofit = createAdapter().build();
service = retrofit.create(IPlusService.class);
}
public static ServiceHelper getInstance() {
return instance;
}
private Retrofit.Builder createAdapter() {
httpClient.setReadTimeout(60, TimeUnit.SECONDS);
httpClient.setConnectTimeout(60, TimeUnit.SECONDS);
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
httpClient.interceptors().add(interceptor);
return new Retrofit.Builder()
.baseUrl(ENDPOINT)
.client(httpClient)
.addConverterFactory(GsonConverterFactory.create());
}
public Call<List<CategoryModel>> getAllCategory() {
return service.getAllCategory();
}
public interface IPlusService {
//@Headers( "Content-Type: application/json" ) in Post method may use this
@GET("/api/category")
Call<List<CategoryModel>> getAllCategory();
}
ServiceHelper.getInstance().getAllCategory().enqueue(new Callback<List<CategoryModel>>() {
@Override
public void onResponse(Response<List<CategoryModel>> response, Retrofit retrofit) {
processResponse(response);
}
@Override
public void onFailure(Throwable t) {
processResponse(null);
}
});
|
Is it good idea to use the Dao design pattern with spring Boot, instead of using repository design pattern provided by s
Tag : java , By : user183275
Date : March 29 2020, 07:55 AM
wish help you to fix your issue The DAO pattern is the same as the Repository Pattern that Spring Data supports. At least, it should be. You have one DAO (=Repository) class per entity that provides methods to query or manipulate that entity. List<User> findByEmailAddressAndLastname(String emailAddress, String lastname);
@Query("select u from User u where u.emailAddress = ?1")
User findByEmailAddress(String emailAddress);
<named-query name="User.findByLastname">
<query>select u from User u where u.lastname = ?1</query>
</named-query>
@Query(name="User.findbyLastname")
List<User> findByLastname(String lastname);
|
Android: how to use Code to interface design pattern to have multiple implementations of network call libraries(Retrofit
Date : March 29 2020, 07:55 AM
it should still fix some issue 1) Methods with callbacks should have no return type. You return null anyway, so make them void. 2) You will eventually need the response back in the UI to update your views, so You should pass the callback as a Callback class, not some String callback. public class NetworkCallsRetrofitImpl implements NetworkCallsApi{
private final Retrofit retrofit;
public NetworkCallsRetrofitImpl() {
retrofit = new Retrofit.Builder()
.baseUrl(StringConstants.ROOT_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
public void getDataFromServer(String url, Callback<ResponseBody> cb) {
retrofit.create(RetrofitApi.class)
.getDataFromServer(url)
.enqueue(cb);
}
}
// This method seems really pointless, by the way...
public void getDataFromServer(String url, Call<ResponseBody> callback){
networkCallsimpl.getDataFromServer(url,callback);
}
public class StatusFragment extends Fragment implements Callback<ResponseBody> {
private ServiceCalls serviceCalls;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Setting the implementation
serviceCalls = ServiceCalls.getInstance();
serviceCalls.setNetworkCallsimpl(new NetworkCallsRetrofitImpl());
}
private void updateStatus() {
serviceCalls.getDataFromServer("http://site.example/status", this);
}
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
String response = response.body().toString();
Toast.makeText(getActivity(), response, TOAST.LENGTH_SHORT).show();
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Log.e("err", t.getMessage());
}
}
|
How to send POST request with data & List<CustomeList> in retrofit follow MVP pattern in android
Date : March 29 2020, 07:55 AM
I wish did fix the issue. , Create models for your request Request class : import com.google.gson.annotations.SerializedName;
import java.util.ArrayList;
import java.util.List;
public class ReqWorkOrder {
@SerializedName("wo_type")
private String woType;
@SerializedName("wo_id")
private String woId;
@SerializedName("issue_date")
private String issueDate;
@SerializedName("issue_branch")
private String issueBranch;
@SerializedName("status")
private String status;
@SerializedName("vehicle_details")
private List<Vehicle> vehicleDetails = new ArrayList<>();
public void setWoType(String woType) {
this.woType = woType;
}
public String getWoType() {
return woType;
}
public void setWoId(String woId) {
this.woId = woId;
}
public String getWoId() {
return woId;
}
public void setIssueDate(String issueDate) {
this.issueDate = issueDate;
}
public String getIssueDate() {
return issueDate;
}
public void setIssueBranch(String issueBranch) {
this.issueBranch = issueBranch;
}
public String getIssueBranch() {
return issueBranch;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public List<Vehicle> getVehicleDetails() {
return vehicleDetails;
}
public void setVehicleDetails(List<Vehicle> vehicleDetails) {
this.vehicleDetails = vehicleDetails;
}
}
import com.google.gson.annotations.SerializedName;
public class Vehicle{
@SerializedName("short_close_qty")
private String shortCloseQty;
@SerializedName("width")
private String width;
@SerializedName("rfb_actual_allowed")
private String rfbActualAllowed;
@SerializedName("height")
private String height;
public void setShortCloseQty(String shortCloseQty){
this.shortCloseQty = shortCloseQty;
}
public String getShortCloseQty(){
return shortCloseQty;
}
public void setWidth(String width){
this.width = width;
}
public String getWidth(){
return width;
}
public void setRfbActualAllowed(String rfbActualAllowed){
this.rfbActualAllowed = rfbActualAllowed;
}
public String getRfbActualAllowed(){
return rfbActualAllowed;
}
public void setHeight(String height){
this.height = height;
}
public String getHeight(){
return height;
}
}
private ReqWorkOrder getBody(){
ReqWorkOrder reqWorkOrder=new ReqWorkOrder();
reqWorkOrder.setWoId(createWOView.getWoId());
reqWorkOrder.setIssueBranch(createWOView.getIssueBranch());
reqWorkOrder.setIssueDate(createWOView.getIssueDate());
reqWorkOrder.setWoType(createWOView.getWOType());
reqWorkOrder.setStatus(createWOView.getStatus());
reqWorkOrder.getVehicleDetails().addAll(createWOView.getVehicleList());
return reqWorkOrder;
}
@POST("WorkOrder/SaveWorkOrder")
Competable createWoRequest(@HeaderMap Map headers, @Body ReqWorkOrder body);
|