Я пытаюсь добавить представление рециркулятора к ответу json.
Я могу успешно напечатать данные, полученные через ответ, в тосте, но я не уверен, как передать их по адаптеру RecyclerView , Большинство из них говорят о Конвертере Gson и ни одного на Моши
Я искал inte rnet некоторое время, но я был беспомощен.
Мне интересно, как передать данные из myactivity (OrderHistoryActivity) в RecyclerViewAdapter (OrderHistoryRecyclerAdapter).
Вот мой код
OrderHistoryActivity
package com.tesmachino.saycure;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import com.tesmachino.saycure.Auth.TokenManager;
import com.tesmachino.saycure.entities.OrderHistory.OrderHistoryGet;
import com.tesmachino.saycure.entities.OrderHistory.OrderHistoryResponse;
import com.tesmachino.saycure.network.ApiService;
import com.tesmachino.saycure.network.RetrofitBuilder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class OrderHistoryActivity extends AppCompatActivity {
ApiService service;
TokenManager tokenManager;
Call<OrderHistoryResponse> call;
private static final String TAG = "OrderHistoryActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order_history);
tokenManager = TokenManager.getInstance(getSharedPreferences("prefs", MODE_PRIVATE));
service = RetrofitBuilder.createServiceWithAuth(ApiService.class, tokenManager);
}
@Override
protected void onResume() {
call = service.orderHistory();
call.enqueue(new Callback<OrderHistoryResponse>() {
@Override
public void onResponse(Call<OrderHistoryResponse> call, Response<OrderHistoryResponse> response) {
Toast.makeText(OrderHistoryActivity.this, "" + response.body().getData(), Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(Call<OrderHistoryResponse> call, Throwable t) {
Log.w(TAG, "onFailure: " + t.getMessage());
Toast.makeText(OrderHistoryActivity.this, "Please Try again after some time", Toast.LENGTH_SHORT).show();
}
});
super.onResume();
}
}
OrderHistoryRecyclerAdapter
package com.tesmachino.saycure;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.tesmachino.saycure.entities.OrderHistory.OrderHistoryResponse;
import java.util.ArrayList;
import java.util.List;
import retrofit2.Call;
public class OrderHistoryRecyclerAdapter extends RecyclerView.Adapter<OrderHistoryRecyclerAdapter.ViewHolder> {
@NonNull
@Override
public OrderHistoryRecyclerAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_order_history, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull OrderHistoryRecyclerAdapter.ViewHolder holder, int position) {
}
@Override
public int getItemCount() {
return 0;
}
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView order_id;
public ViewHolder(@NonNull View itemView) {
super(itemView);
order_id = (TextView) itemView.findViewById(R.id.order_id);
}
}
}
OrderHistoryResponse
package com.tesmachino.saycure.entities.OrderHistory;
import com.squareup.moshi.Json;
import org.apache.commons.lang3.builder.ToStringBuilder;
import java.util.List;
public class OrderHistoryResponse {
@Json(name = "data")
private List<OrderHistoryGet> data = null;
public List<OrderHistoryGet> getData() {
return data;
}
public void setData(List<OrderHistoryGet> data) {
this.data = data;
}
@Override
public String toString() {
return new ToStringBuilder(this).append("data", data).toString();
}
}
OrderHistoryGet
package com.tesmachino.saycure.entities.OrderHistory;
import com.squareup.moshi.Json;
import org.apache.commons.lang3.builder.ToStringBuilder;
import java.util.List;
public class OrderHistoryGet {
@Json(name = "id")
private Integer id;
@Json(name = "payment_callback_id")
private Integer paymentCallbackId;
@Json(name = "payment_orderid")
private Integer paymentOrderid;
@Json(name = "customer_id")
private Integer customerId;
@Json(name = "service_type_id")
private Integer serviceTypeId;
@Json(name = "number_of_cameras")
private Integer numberOfCameras;
@Json(name = "camera_type")
private Integer cameraType;
@Json(name = "dvr_channel")
private Integer dvrChannel;
@Json(name = "paid_amount")
private Integer paidAmount;
@Json(name = "is_paid")
private Integer isPaid;
@Json(name = "status")
private Integer status;
@Json(name = "deleted_at")
private Object deletedAt;
@Json(name = "created_at")
private String createdAt;
@Json(name = "updated_at")
private String updatedAt;
@Json(name = "service_assign_status")
private List<OrderHistoryServiceAssign> serviceAssignStatus = null;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getPaymentCallbackId() {
return paymentCallbackId;
}
public void setPaymentCallbackId(Integer paymentCallbackId) {
this.paymentCallbackId = paymentCallbackId;
}
public Integer getPaymentOrderid() {
return paymentOrderid;
}
public void setPaymentOrderid(Integer paymentOrderid) {
this.paymentOrderid = paymentOrderid;
}
public Integer getCustomerId() {
return customerId;
}
public void setCustomerId(Integer customerId) {
this.customerId = customerId;
}
public Integer getServiceTypeId() {
return serviceTypeId;
}
public void setServiceTypeId(Integer serviceTypeId) {
this.serviceTypeId = serviceTypeId;
}
public Integer getNumberOfCameras() {
return numberOfCameras;
}
public void setNumberOfCameras(Integer numberOfCameras) {
this.numberOfCameras = numberOfCameras;
}
public Integer getCameraType() {
return cameraType;
}
public void setCameraType(Integer cameraType) {
this.cameraType = cameraType;
}
public Integer getDvrChannel() {
return dvrChannel;
}
public void setDvrChannel(Integer dvrChannel) {
this.dvrChannel = dvrChannel;
}
public Integer getPaidAmount() {
return paidAmount;
}
public void setPaidAmount(Integer paidAmount) {
this.paidAmount = paidAmount;
}
public Integer getIsPaid() {
return isPaid;
}
public void setIsPaid(Integer isPaid) {
this.isPaid = isPaid;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public Object getDeletedAt() {
return deletedAt;
}
public void setDeletedAt(Object deletedAt) {
this.deletedAt = deletedAt;
}
public String getCreatedAt() {
return createdAt;
}
public void setCreatedAt(String createdAt) {
this.createdAt = createdAt;
}
public String getUpdatedAt() {
return updatedAt;
}
public void setUpdatedAt(String updatedAt) {
this.updatedAt = updatedAt;
}
public List<OrderHistoryServiceAssign> getServiceAssignStatus() {
return serviceAssignStatus;
}
public void setServiceAssignStatus(List<OrderHistoryServiceAssign> serviceAssignStatus) {
this.serviceAssignStatus = serviceAssignStatus;
}
@Override
public String toString() {
return new ToStringBuilder(this).append("id", id).append("paymentCallbackId", paymentCallbackId).append("paymentOrderid", paymentOrderid).append("customerId", customerId).append("serviceTypeId", serviceTypeId).append("numberOfCameras", numberOfCameras).append("cameraType", cameraType).append("dvrChannel", dvrChannel).append("paidAmount", paidAmount).append("isPaid", isPaid).append("status", status).append("deletedAt", deletedAt).append("createdAt", createdAt).append("updatedAt", updatedAt).append("serviceAssignStatus", serviceAssignStatus).toString();
}
}
apiService
package com.tesmachino.saycure.network;
import com.tesmachino.saycure.entities.OrderHistory.OrderHistoryResponse;
import java.util.List;
import retrofit2.Call;
import retrofit2.http.GET;
public interface ApiService {
@GET("order-history")
Call<OrderHistoryResponse> orderHistory();
}
RetrofitBuilder
package com.tesmachino.saycure.network;
import com.facebook.stetho.okhttp3.StethoInterceptor;
import com.tesmachino.saycure.Auth.TokenManager;
import com.tesmachino.saycure.BuildConfig;
import java.io.IOException;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.Retrofit;
import retrofit2.converter.moshi.MoshiConverterFactory;
public class RetrofitBuilder {
private static final String BASE_URL = "http://192.168.1.11/say-cure-web/public/api/";
// private static final String BASE_URL = "http://saycure.tesmachino.xyz/api/";
private final static OkHttpClient client = buildClient();
private final static Retrofit retrofit = buildRetrofit(client);
private static OkHttpClient buildClient() {
OkHttpClient.Builder builder = new OkHttpClient.Builder()
.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Request.Builder builder = request.newBuilder()
.addHeader("Accept", "application/json")
.addHeader("Connection", "close");
request = builder.build();
return chain.proceed(request);
}
});
if (BuildConfig.DEBUG) {
builder.addNetworkInterceptor(new StethoInterceptor());
}
return builder.build();
}
private static Retrofit buildRetrofit(OkHttpClient client) {
return new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client)
.addConverterFactory(MoshiConverterFactory.create())
.build();
}
public static <T> T createService(Class<T> service) {
return retrofit.create(service);
}
public static <T> T createServiceWithAuth(Class<T> service, TokenManager tokenManager) {
OkHttpClient newClient = client.newBuilder().addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Request.Builder builder = request.newBuilder();
if (tokenManager.getToken().getAccessToken() != null) {
builder.addHeader("Authorization", "Bearer " + tokenManager.getToken().getAccessToken());
}
request = builder.build();
return chain.proceed(request);
}
}).build();
Retrofit newRetroFit = retrofit.newBuilder().client(newClient).build();
return newRetroFit.create(service);
}
public static Retrofit getRetrofit() {
return retrofit;
}
}
Любая помощь по этому вопросу была бы очень признательна. Если мой вопрос неясен, пожалуйста, дайте мне знать, я был бы рад перефразировать его. Пожалуйста, объясните мне, как это работало в конце