如何使用 Volley 进行并发 GET 调用?

我有 3 个 API GET 调用。我的方法面临的问题是,应用程序能够从两个 API 成功获取数据,并且我也能够在 UI 上显示它。但是,对于第三次 API 调用,由于以下错误,之前显示的数据消失了,这很糟糕。

D/Volley: [380] BasicNetwork.logSlowRequests: HTTP response for request=<[ ] http://example.com/api/search/getTwitterData?limit=10&tag=JavaScript 0x865f5dc2 NORMAL 3> [lifetime=6683], [size=10543], [rc=200], [retryCount=0]

如何使用 Volley 进行并发 API GET 调用而不丢失 UI 上的数据。有人可以指导我吗?

这是我的代码的摘录。


炎炎设计
浏览 134回答 4
4回答

Helenr

问题是您每次都会初始化适配器,这就是为什么一旦新的 API 调用您的数据就会丢失。我更喜欢下面的方法,这样可以帮助您,在 ArrayList 中添加数据并通知适配器,在onCreate中添加这一行,staggeredGridAdapter = new StaggeredGridAdapter(StaggeredSearchActivity.this, dataset);recyclerView.setAdapter(staggeredGridAdapter);API回调响应的变化:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;...dataset.add(new StaggeredCustomCard(user, userpost, postdate));循环后添加以下行staggeredGridAdapter.notifyDataSetChanged();适配器的变化&nbsp;private ArrayList<StaggeredCustomCard> dataSet;&nbsp; &nbsp; &nbsp; &nbsp; private Context context;&nbsp; &nbsp; &nbsp; &nbsp; public MyAdapter(ArrayList<StaggeredCustomCard> dataSet, Context context) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.data = data;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.context = context;&nbsp; &nbsp; &nbsp; &nbsp; }注意:不要在适配器上创建新对象。

MYYA

这种方法没有多大意义。一旦您从三个端点之一收到响应,您似乎每次都会创建一个新的适配器并将其附加到回收器并使用随机的“notifyDataSetChanged”...也许可以考虑使用具有处理业务登录的服务层和网络层的 ViewModel。当网络方法之一的回调从端点响应时,ViewModel 更新/发布 MutableLiveData>...合并三部分数据。该活动只是观察 ViewModel 的 MutableLiveData 并使用 DiffUtil 来更新回收器中的外观/卡片。

缥缈止盈

最好的方法是在 onCreate 方法的 StaggeredSearchActivity 中注册您的 LiveData 并像您一样监听数据库更改。在每个成功响应中,将其结果保存到没有 LiveData 的数据库中。onCreate方法中的LiveData会被触发。&nbsp; &nbsp;@Override&nbsp; protected void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; &nbsp; &nbsp; setContentView(R.layout.activity_main2);&nbsp; ViewModel viewModel = ViewModelProviders.of(this, factory).get(ViewModel.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; viewModel.getEntity().observe(this, entity -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (entity != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; adapter.notifyDataSetChanged(entity );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; requestQueue = Volley.newRequestQueue(this);&nbsp; &nbsp; Intent intent = getIntent();&nbsp; &nbsp; String searchText = intent.getStringExtra("searchText");&nbsp; &nbsp; // Three concurrent API GET Calls&nbsp; &nbsp; getMediumData(searchText);&nbsp; &nbsp; getExampleData(searchText);&nbsp; &nbsp; getGoogleData(searchText);&nbsp; &nbsp; recyclerView = findViewById(R.id.staggered_recycler_view);&nbsp; &nbsp; staggeredGridLayoutManager = new StaggeredGridLayoutManager(2, LinearLayoutManager.VERTICAL);&nbsp; &nbsp; recyclerView.setLayoutManager(staggeredGridLayoutManager);}ArrayList<StaggeredCustomCard> dataset = new ArrayList<>();private void getMediumData(String searchText) {&nbsp; &nbsp; progressBar = findViewById(R.id.progressBar);&nbsp; &nbsp; progressBar.setVisibility(View.VISIBLE);&nbsp; &nbsp; String url = UrlConstants.getUrl() + searchText;&nbsp; &nbsp; JsonObjectRequest jsonObjectRequest = new JsonObjectRequest&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (Request.Method.GET, url, null, new Response.Listener<JSONObject>() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onResponse(JSONObject response) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; StaggeredCustomCardDAO.insert();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // TODO just insert to dataBase&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }, new Response.ErrorListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onErrorResponse(VolleyError error) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // TODO: Handle error&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; progressBar.setVisibility(View.INVISIBLE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; jsonObjectRequest.setRetryPolicy(new&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DefaultRetryPolicy(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 15,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DefaultRetryPolicy.DEFAULT_MAX_RETRIES,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));&nbsp; &nbsp; &nbsp; &nbsp; requestQueue.add(jsonObjectRequest);&nbsp; &nbsp; }&nbsp; &nbsp; private void getExampleData(String searchText) {&nbsp; &nbsp; &nbsp; &nbsp; JsonArrayRequest jsonArrayRequest = new JsonArrayRequest&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (Request.Method.GET, url, null, new Response.Listener<JSONArray>() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onResponse(JSONArray response) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // TODO just insert to dataBase&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; StaggeredCustomCardDAO.insert();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }, new Response.ErrorListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onErrorResponse(VolleyError error) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; jsonArrayRequest.setRetryPolicy(new DefaultRetryPolicy(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 15,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DefaultRetryPolicy.DEFAULT_MAX_RETRIES,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));&nbsp; &nbsp; &nbsp; &nbsp; requestQueue.add(jsonArrayRequest);&nbsp; &nbsp; }&nbsp; &nbsp; private void getGoogleData(String searchText) {&nbsp; &nbsp; &nbsp; &nbsp; JsonObjectRequest jsonObjectRequest = new JsonObjectRequest&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (Request.Method.GET, url, null, new Response.Listener<JSONObject>() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onResponse(JSONObject response) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // TODO just insert to dataBaseStaggeredCustomCardDAO.insert();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }, new Response.ErrorListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onErrorResponse(VolleyError error) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 15,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DefaultRetryPolicy.DEFAULT_MAX_RETRIES,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));&nbsp; &nbsp; &nbsp; &nbsp; requestQueue.add(jsonObjectRequest);&nbsp; &nbsp; }&nbsp; &nbsp; class ViewModel extends androidx.lifecycle.ViewModel {&nbsp; &nbsp; &nbsp; &nbsp; private LiveData<StaggeredCustomCard> entity;&nbsp; &nbsp; &nbsp; &nbsp; public ViewModel(Repository repository) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; entity = repository.getNetworkData();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public LiveData<StaggeredCustomCard> getEntity() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return entity;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; class Repository {&nbsp; &nbsp; &nbsp; &nbsp; LiveData<StaggeredCustomCard> getNetworkData() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LiveData<StaggeredCustomCard> localeData =&nbsp; StaggeredCustomCardDAO .getLocaleData();//... todo Read from data base&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return localeData;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; @Dao&nbsp; &nbsp; public interface StaggeredCustomCardDAO {&nbsp; &nbsp; &nbsp; &nbsp; @Query("Select * from tbl_staggeredCustomCard ")&nbsp; &nbsp; &nbsp; &nbsp; LiveData<StaggeredCustomCard> getLocaleData();&nbsp; &nbsp; &nbsp; &nbsp; @Insert(onConflict = OnConflictStrategy.REPLACE)&nbsp; &nbsp; &nbsp; &nbsp; void insert(List<StaggeredCustomCard> items);&nbsp; &nbsp; }&nbsp; &nbsp; }

达令说

第一种方法:首先我建议您创建一个中央请求队列。public class AppController extends Application {&nbsp; &nbsp; public static final String TAG = AppController.class&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .getSimpleName();&nbsp; &nbsp; private RequestQueue mRequestQueue;&nbsp; &nbsp; private static AppController mInstance;&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onCreate() {&nbsp; &nbsp; &nbsp; &nbsp; super.onCreate();&nbsp; &nbsp; &nbsp; &nbsp; mInstance = this;&nbsp; &nbsp; }&nbsp; &nbsp; public static synchronized AppController getInstance() {&nbsp; &nbsp; &nbsp; &nbsp; return mInstance;&nbsp; &nbsp; }&nbsp; &nbsp; public RequestQueue getRequestQueue() {&nbsp; &nbsp; &nbsp; &nbsp; if (mRequestQueue == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mRequestQueue = Volley.newRequestQueue(getApplicationContext());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return mRequestQueue;&nbsp; &nbsp; }&nbsp; &nbsp; public <T> void addToRequestQueue(Request<T> req, String tag) {&nbsp; &nbsp; &nbsp; &nbsp; // set the default tag if tag is empty&nbsp; &nbsp; &nbsp; &nbsp; req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);&nbsp; &nbsp; &nbsp; &nbsp; getRequestQueue().add(req);&nbsp; &nbsp; }&nbsp; &nbsp; public <T> void addToRequestQueue(Request<T> req) {&nbsp; &nbsp; &nbsp; &nbsp; req.setTag(TAG);&nbsp; &nbsp; &nbsp; &nbsp; getRequestQueue().add(req);&nbsp; &nbsp; }&nbsp; &nbsp; public void cancelPendingRequests(Object tag) {&nbsp; &nbsp; &nbsp; &nbsp; if (mRequestQueue != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mRequestQueue.cancelAll(tag);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}然后将您想要的请求添加到队列中// Adding request to request queueAppController.getInstance().addToRequestQueue(jsonObjReq);第二种方式:创建一个Generic Volley类和一个接口,使用接口来获取成功和失败响应。步骤 1 创建一个单独的 Volley 类 步骤 2 创建一个用于访问 volley 类响应的接口 步骤 3 为该类创建新对象并发送所需参数 new PostVolleyJsonRequest(TestVolley.this, TestVolley.this(interfcae), "Submit", url ,参数);类的上下文 用于发送成功和失败响应的接口 成功时识别的请求类型 url(强制) GET 的参数(可选) 不需要 通用 volley 类public class PostVolleyJsonRequest {private String&nbsp; type;private Activity act;private VolleyJsonRespondsListener volleyJsonRespondsListener;private String networkurl;private JSONObject jsonObject = null;private JSONObject params;public PostVolleyJsonRequest(Activity act, VolleyJsonRespondsListener volleyJsonRespondsListener, String type, String netnetworkUrl,JSONObject params) {&nbsp; &nbsp; this.act = act;&nbsp; &nbsp; this.volleyJsonRespondsListener = volleyJsonRespondsListener;&nbsp; &nbsp; this.type = type;&nbsp; &nbsp; this.networkurl = netnetworkUrl;&nbsp; &nbsp; this.params = params;&nbsp; &nbsp; sendRequest();}private void sendRequest() {&nbsp; &nbsp; Log.d("url", "url" + networkurl);&nbsp; &nbsp; JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST,networkurl,params,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Response.Listener<JSONObject>() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onResponse(JSONObject response) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.e("response", "response " + response);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; volleyJsonRespondsListener.onSuccessJson(response, type);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Response.ErrorListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onErrorResponse(VolleyError error) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NetworkResponse response = error.networkResponse;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.e("response", "response " + response);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (response != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int code = response.statusCode;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String errorMsg = new String(response.data);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.e("response", "response" + errorMsg);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jsonObject = new JSONObject(errorMsg);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (JSONException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String msg = jsonObject.optString("message");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; volleyJsonRespondsListener.onFailureJson(code, msg);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String errorMsg = error.getMessage();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; volleyJsonRespondsListener.onFailureJson(0, errorMsg);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; jsObjRequest.setRetryPolicy(new DefaultRetryPolicy(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 600000,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DefaultRetryPolicy.DEFAULT_MAX_RETRIES,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));&nbsp; &nbsp; RequestQueue requestqueue = Volley.newRequestQueue(act);&nbsp; &nbsp; requestqueue.add(jsObjRequest);}}Use the interface to get responds messagepublic interface VolleyJsonRespondsListener {public void onSuccessJson(JSONObject result, String type);public void onFailureJson(int responseCode, String responseMessage);}在您想要包含多个请求的班级中public class TestVolley extends AppCompatActivity implements VolleyJsonRespondsListener{//Your class code goes here//network requesttry {&nbsp; &nbsp; &nbsp; &nbsp; //parameters&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; //Context,Interface,Type(to indentify your responds),URL,parameter for your request&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; //request 1&nbsp; &nbsp; &nbsp; &nbsp; new PostVolleyJsonRequest(TestVolley.this, TestVolley.this, "Submit", url, params);&nbsp; &nbsp; &nbsp; &nbsp; //request 2&nbsp; &nbsp; &nbsp; &nbsp; new PostVolleyJsonRequest(TestVolley.this, TestVolley.this, "AccessData", url_2, params_2);&nbsp;} catch (Exception e) {&nbsp;e.printStackTrace()&nbsp;}&nbsp;//Methods from Interface&nbsp; @Overridepublic void onSuccessJson(JSONObject result, String type) {&nbsp; &nbsp;//Based on the Type you send get the responds and parse it&nbsp;&nbsp; &nbsp; switch (type) {&nbsp; &nbsp; &nbsp; &nbsp; case "Submit":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parseSubmit(result);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception e) {
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java