有没有办法在RxJava 2中使用Android Volley?

时间:2019-12-03 05:57:34

标签: android multithreading android-volley rx-java2 rx-android

我只有一个Volley构造函数类,可用于在应用程序中调用多个网络操作。我曾尝试使用Retrofit,但是不知何故,网络通话比截击慢,我更喜欢使用Volley。我想知道我是否可以将RxJava 2与Volley一起使用以在后台线程中处理网络操作。以下是我的排球课:

public class VolleyWebService {

public interface VolleyStringResponseListener {

    void onSuccess(String response);

    void onError(String error);
}
public void postResponse(String url, final HashMap<String, String> params, final Context mContext, final VolleyStringResponseListener listener) {
    try {
        RequestQueue mQueue = Volley.newRequestQueue(mContext);

        System.out.println("Login Thread "+Thread.currentThread());
        StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                listener.onSuccess(response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                if (error.networkResponse == null) {
                    listener.onError("Bad URL Request");
                } else if (error.networkResponse.statusCode == 404) {
                    listener.onError("Server Error");
                } else {
                    listener.onError(error.getMessage());
                }
            }
        }) {
            @Override
            protected HashMap<String, String> getParams() {
                return params;
            }

            @Override
            public Map<String, String> getHeaders() {
                Map<String, String> params = new HashMap<>();
                params.put("Content-Type", "application/x-www-form-urlencoded");
                return params;
            }
        };
        mQueue.getCache().clear();
        request.setRetryPolicy(new DefaultRetryPolicy(0, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_MAX_RETRIES));
        mQueue.add(request);
    } catch (Exception ignored) {
    }
}

}

我实现了Volley类的上传照片

    private void UploadPhoto() {
    final ProgressDialog pDialog;
    pDialog = new ProgressDialog(this);
    pDialog.setMessage("Synchronizing..");
    pDialog.setCancelable(false);
    pDialog.show();

    HashMap<String, String> params = new HashMap<>();
    params.put("Id", _id);
    params.put("imageNo", _imageNumber);
    params.put("image", _image);

    new VolleyWebService().postResponse(urlConfigClass.UPLOAD_PHOTO, params, this, new VolleyWebService.VolleyStringResponseListener() {
        @Override
        public void onSuccess(String response) {
            try {
                JSONObject jsonRootObject = new JSONObject(response);
                String status = jsonRootObject.getString("status");
                String error_msg = jsonRootObject.getString("errorMsg");
                if (status.equals("1")) {
                    pDialog.dismiss();
                    Toasty.success(getApplicationContext(), server_msg, Toast.LENGTH_LONG).show();
                } else if (status.equals("0")) {
                    pDialog.dismiss();
                    Toasty.error(getApplicationContext(), error_msg, Toast.LENGTH_LONG).show();
                } else {
                    pDialog.dismiss();
                    Toasty.error(getApplicationContext(), response, Toast.LENGTH_LONG).show();
                }
            } catch (Exception ex) {
                pDialog.dismiss();
                Toasty.error(getApplicationContext(), Objects.requireNonNull(ex.getMessage()), Toast.LENGTH_LONG).show();
            }
        }

        @Override
        public void onError(String message) {
            pDialog.dismiss();
            Toasty.error(getApplicationContext(), "Server Response: " + message, Toast.LENGTH_LONG).show();
        }
    });
}

0 个答案:

没有答案