public class MovieFragment extends Fragment implements getData { TextView testText; ImageView imageView;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.moviefragment, container, false);
testText = rootView.findViewById(R.id.textView);
imageView = rootView.findViewById(R.id.image);
if (AppHelper.requestQueue == null) {
AppHelper.requestQueue = Volley.newRequestQueue(getContext());
}
/kun requestMovieList ("http://boostcourse-appapi.connect.or.kr:10000/movie/readMovie?id=1");
/Justice League requestMovieList ("http://boostcourse-appapi.connect.or.kr:10000/movie/readMovie?id=2");
/Thor requestMovieList ("http://boostcourse-appapi.connect.or.kr:10000/movie/readMovie?id=3");
/Loving Vincent requestMovieList ("http://boostcourse-appapi.connect.or.kr:10000/movie/readMovie?id=4");
/ RequestMovieList ("http://boostcourse-appapi.connect.or.kr:10000/movie/readMovie?id=5");
return rootView;
}
@Override
public void requestMovieList(String data) {
String url = data;
StringRequest request = new StringRequest(
Request.Method.GET,
url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
processResponse(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.getMessage();
}
}
);
request.setShouldCache(false);
AppHelper.requestQueue.add(request);
}
public void processResponse(String response) {
Gson gson = new Gson();
ResponseInfo info = gson.fromJson(response, ResponseInfo.class);
if (info.code == 200) {
MovieList movieList = gson.fromJson(response, MovieList.class);
if (movieList != null) {
MovieInfo movieInfo = movieList.result.get(0);
println(movieInfo.title);
}
}
}
public void println(String data) {
testText.append(data + "\n");
}
}
Here's the current situation
In the requestMovieList() method, enter a String value for the response URL.
Convert the transferred value to a Java object with Gson and then receive the title of the movie in turn in a simple TextView in the fragment
Output as a println() function
The question is, in the order of the movies in the answer URL, GUN -> Justice League -> Thor -> Loving Vincent -> Crime City
It has to come out in order, but it's a mess.
android volley gson synchronized
Because it's asynchronous, it's listed in the order in which the responses are received quickly, regardless of the order in which it's queued, so the order will be mixed up every time.
Unless you're going to use a separate library There are two ways you can take it.
1. Series Method
How to execute the following request each time a response is received. This approach is inefficient, but intuitive, because you have to wait for all the latency of networking that happens before you get all the results.
2. Parell method
In the structure you just created, you continue to hand over the number of requests to the parameter, and when you receive them at the end, you can sort with this value or put them directly into the index of the desired result array. If you have enough network socket pools to run all requests at the same time, you only have to wait for the longest request out of the five.
© 2024 OneMinuteCode. All rights reserved.