Android Volley多个请求

Android Volley多个请求,第1张

概述我尝试在当前的排球请求中执行新的排球请求,但是当调用新请求时,它不会进入onrespond方法.新请求应在第一次结束之前执行.(后进先出)如何成功执行新请求?privatevoidmakeJsonObjectRequest(){ac=newAppController();finalJsonObjectRequestjsonObjReq=ne

我尝试在当前的排球请求中执行新的排球请求,但是当调用新请求时,它不会进入onrespond方法.

新请求应在第一次结束之前执行. (后进先出)

如何成功执行新请求?

private voID makeJsonObjectRequest() {    ac = new AppController();    final JsonObjectRequest JsonObjReq = new JsonObjectRequest(Request.Method.GET,            url, null, new Response.Listener<JsONObject>() {        @OverrIDe        public voID onResponse(JsONObject response) {            Log.d("test", response.toString());            try {                // Parsing Json object response                // response will be a Json object                JsONArray name = response.getJsONArray("data");                for (int i = 0; i < name.length(); i++) {                    JsONObject post = (JsONObject) name.getJsONObject(i);                    try {                        objectID = post.getString("object_ID");                        newRequest(objectID);                    }                    catch (Exception e) {                   }                }            } catch (JsONException e) {                e.printstacktrace();            }        }    }, new Response.ErrorListener() {        @OverrIDe        public voID one rrorResponse(VolleyError error) {            VolleyLog.d("test", "Error: " + error.getMessage());        }    });    // Adding request to request queue    ac.getInstance().addToRequestQueue(JsonObjReq);}

解决方法:

试一试100%

public class Utility {String result = "";String tag_string_req = "string_raq";private Activity activity;Context context;private linearLayout mlinear;private ProgressDialog pDialog;public Utility(Context context) {    this.context = context;}public String getString(String url, final VolleyCallback callback) {    showpDialog();    StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {        @OverrIDe        public voID onResponse(String response) {            result = response;            hIDeDialog();            callback.onSuccess(response);        }    }, new Response.ErrorListener() {        @OverrIDe        public voID one rrorResponse(VolleyError error) {            callback.onRequestError(error);            hIDeDialog();           /*LayoutInflater inflater = ((Activity) context).getLayoutInflater();            VIEw layout = inflater.inflate(R.layout.custom_toast, null);            ((Activity) context).setContentVIEw(layout);*/        }    });    VolleySingleton.getInstance().addToRequestQueue(stringRequest, tag_string_req);    stringRequest.setRetryPolicy(            new DefaultRetryPolicy(1 * 1000, 1, 1.0f));    return result;}public interface VolleyCallback {    voID onSuccess(String result);    voID onRequestError(VolleyError errorMessage);    //voID onjsonInvoke(String url, final VolleyCallback callback);}public boolean isOnline() {    Runtime runtime = Runtime.getRuntime();    try {        Process ipProcess = runtime.exec("/system/bin/Ping -c 1 8.8.8.8");        int exitValue = ipProcess.waitFor();        return (exitValue == 0);    } catch (IOException e) {        e.printstacktrace();    } catch (InterruptedException e) {        e.printstacktrace();    }    return false;}private voID showpDialog() {    onProgress();    if (!pDialog.isShowing())        pDialog.show();}private voID hIDeDialog() {    if (pDialog.isShowing())        pDialog.dismiss();}public voID onProgress() {    pDialog = new ProgressDialog(context);    pDialog.setMessage("Please wait...");    pDialog.setCancelable(false);    pDialog.requestwindowFeature(Window.FEATURE_NO_Title);}

}

叫片段

Utility utility = new Utility(getContext());    utility.getString(urls, new Utility.VolleyCallback() {        @OverrIDe        public voID onSuccess(String result) {            try {                JsONObject toplinks = new JsONObject(result);                JsONObject data  = toplinks.getJsONObject("toplinks");                M.i("============LS", "" + data);            } catch (JsONException e) {                e.printstacktrace();            }            finally {            }        }        @OverrIDe        public voID onRequestError(VolleyError errorMessage) {            errorjson.setVisibility(VIEw.VISIBLE);            String msg = VolleyException.getErrorMessageFromVolleyError(errorMessage);            errorjson.setText(msg);        }    });
总结

以上是内存溢出为你收集整理的Android Volley多个请求全部内容,希望文章能够帮你解决Android Volley多个请求所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1118126.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存