https://launchlibrary.net/1.3/launch показывает json данные. Если вы вставите идентификатор после этого URL, появится более подробная информация о запуске, например, https://launchlibrary.net/1.3/launch/2053.
Я хочу получить URL-адрес изображения соответствующего запуска, но он не работает так, как я это делаю. Это код, из которого я анализирую данные JSON.
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
v = inflater.inflate(R.layout.activity_launches, container, false);
myRecyclerView = (RecyclerView) v.findViewById(R.id.recycler_view);
myRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
launchesList = new ArrayList<>();
temporaryList = new ArrayList<>();
myRequestQueue = Volley.newRequestQueue(getContext());
parseJSON();
return v;
}
private void parseJSON(){
String url = "https://launchlibrary.net/1.3/launch";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("launches");
for(int i = 0; i < jsonArray.length(); i++){
JSONObject launch = jsonArray.getJSONObject(i);
String launchName = launch.getString("name");
String launchDate = launch.getString("net");
launchId = launch.getString("id");
String imageUrl = getImageUrl();
Launch newLaunch = new Launch(launchName, launchDate, imageUrl);
launchesList.add(newLaunch);
}
myRecyclerViewAdapter = new RecyclerViewAdapter(getContext(), launchesList);
myRecyclerView.setAdapter(myRecyclerViewAdapter);
myRecyclerViewAdapter.setOnItemClickListener(LaunchesActivity.this);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
myRequestQueue.add(request);
}
public String getImageUrl(){
String url = "https://launchlibrary.net/1.3/launch/" + launchId;
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("launches");
JSONObject launch = jsonArray.getJSONObject(0);
JSONObject rocket = launch.getJSONObject("rocket");
String url = rocket.getString("imageURL");
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
myRequestQueue.add(request);
return url;
}
Это адаптер, который я создал для изменения текста в TextViews и ImageView.
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.RecyclerViewHolder>{
private Context myContext;
private ArrayList<Launch> launchList;
private OnItemClickListener myListener;
public interface OnItemClickListener{
void onItemClick(int position);
}
public void setOnItemClickListener(OnItemClickListener listener){
myListener = listener;
}
public RecyclerViewAdapter(Context context, ArrayList<Launch> launchList){
myContext = context;
this.launchList = launchList;
}
@NonNull
@Override
public RecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(myContext).inflate(R.layout.launch_item, parent, false);
return new RecyclerViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull RecyclerViewHolder holder, int position) {
Launch currentItem = launchList.get(position);
String name = currentItem.getName();
String date = currentItem.getDate();
String imageUrl = currentItem.getImageUrl();
holder.myTextViewName.setText(name);
holder.myTextViewDate.setText(date);
Picasso.get().load(imageUrl).fit().centerInside().into(holder.myImageViewImage);
}
@Override
public int getItemCount() {
return launchList.size();
}
public class RecyclerViewHolder extends RecyclerView.ViewHolder{
public TextView myTextViewName;
public TextView myTextViewDate;
public ImageView myImageViewImage;
public RecyclerViewHolder(@NonNull View itemView) {
super(itemView);
myTextViewName = itemView.findViewById(R.id.launch_name);
myTextViewDate = itemView.findViewById(R.id.date);
myImageViewImage = itemView.findViewById(R.id.thumbnail);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(myListener != null){
int position = getAdapterPosition();
if(position != RecyclerView.NO_POSITION){
myListener.onItemClick(position);
}
}
}
});
}
}
Запуск объект просто такой:
public Launch(String name, String date, String imageUrl) {
this.name = name;
this.date = date;
this.imageUrl = imageUrl;
}
Что я делаю не так?
Макет элемента Recyclerview:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_marginTop="5dp"
android:padding="8dp"
android:background="#323131">
<ImageView
android:id="@+id/thumbnail"
android:layout_width="100dp"
android:layout_height="match_parent"
android:scaleType="centerCrop"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="130dp"
android:layout_margin="8dp"
android:orientation="vertical">
<TextView
android:id="@+id/launch_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Launch name"
android:textSize="18sp"
android:textStyle="bold"
android:textColor="@android:color/white"/>
<TextView
android:id="@+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Date"
android:textColor="@android:color/white"/>
</LinearLayout>
Макет Recyclerview
Recyclerview layout:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recycler_view"
android:background="#323131"/>