- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
这是一个简单的 Volley Api 监听器,如何从单个 Api 调用方法中调用我项目中的每个 API。我在一个项目中有大约 40 个其他类似的 API,我厌倦了一次又一次地编写相同的代码块。有谁知道该怎么做?==> 我听说进行回调是一种解决方案,但我想要确切的方法来处理它。
private void reservationCancelAPIExecute(String reservation_id) {
CustomProgressBarDialog.progressDialog = CustomProgressBarDialog.showProgressDialog(mcontext);
if (visible) CustomProgressBarDialog.progressDialog.show();
final HashMap<String, String> params = new HashMap<>();
params.put("user_id", user_ID);
params.put("reservation_id", reservation_id);
Log.e("Parameters", params.toString());
String REquestUrl = Config.APP_BASE_URL + Config.API_RESERVATION_CANCEL;
StringRequest req = new StringRequest(Request.Method.POST, REquestUrl,
new Response.Listener<String>() {
@Override
public void onResponse(String responseObject) {
String newResponse = responseObject;
CustomProgressBarDialog.progressDialog.dismiss();
if (responseObject.contains("")) {
newResponse = newResponse.substring(newResponse.indexOf("{"));
}
try {
JSONObject response = new JSONObject(newResponse);
boolean error = response.getBoolean("error");
if (!error) {
CustomProgressBarDialog.progressDialog.dismiss();
session.createReservationByUser(DBTableFields.RESERVATION_STATE_CANCELLED);
session.createReservationByUser(DBTableFields.RESERVATION_STATE_NEW);
//EApplication.getInstance().showToastMessageFunction(response.getString("message"));
/* session.clearPickUPDropOffLocationAddress();*/
EApplication.getInstance().setPreviousReservationStatus(false);
/* reservationCancelSuccessDialog();*/
if (isAdded()) {
EApplication.getInstance().showToastMessageFunction(getResources().getString(R.string.canceled_reservation));
}
//Toast.makeText(mcontext, getResources().getString(R.string.canceled), Toast.LENGTH_SHORT).show();
} else
{
EApplication.getInstance().showToastMessageFunction("" + response.getString("message"));
CustomProgressBarDialog.progressDialog.dismiss();
}
} catch (JSONException e) {
e.printStackTrace();
CustomProgressBarDialog.progressDialog.dismiss();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
if(isAdded())
EApplication.getInstance().showToastMessageFunction(getResources().getString(R.string.no_internet_availeble));
VolleyLog.d("Error", "Error: " + error.getMessage());
CustomProgressBarDialog.progressDialog.dismiss();
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
return params;
}
@Override
public Request.Priority getPriority() {
return Request.Priority.HIGH;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
/*headers.put("Content-Type", "application/json");*/
String current_language = session.getAppLanguage();
Log.i("current_language", current_language);
if (current_language.equalsIgnoreCase(Config.LANG_ENG)) {
language = "en";
}
if (current_language.equalsIgnoreCase(Config.LANG_THAI)) {
language = "np";
}
headers.put("lang", language);
return headers;
}
};
req.setRetryPolicy(new DefaultRetryPolicy(
20000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
EApplication.getInstance().addToRequestQueue(req, "high");
}
Config文件包含,即API列表
package com.EddyCabLatest.application.constant;
public class Config {
public static final String PRIVACY_POLICY_DRIVER_NEPALI =
"<a href=https://www.eddycab.com/np/legal-2/privacy/drivers/";
public static final String PRIVACY_POLICY_DRIVER_ENGLISH =
"<a href=https://www.eddycab.com/en/legal/privacy/drivers/";
public static final String PRIVACY_POLICY_PASSENGER_ENGLISH = "<a href=https://www.eddycab.com/en/legal/privacy/users/";
public static final String PRIVACY_POLICY_PASSENGER_NEPALI =
"<a href=https://www.eddycab.com/np/legal-2/privacy/users/";
public static final String TERMS_DRIVER_NEPALI =
"<a href=https://www.eddycab.com/np/legal-2/user-terms/";
public static final String TERMS_DRIVER_ENGLISH =
"<a href=https://www.eddycab.com/en/legal/terms-conditions/";
public static final String GOOGLE_MAP_API_KEY = "AIzaSyAi6qreeIcyoDDxtrcMnYqKwTMR064C4xc";//test
public static final String GCM_SENDER_KEY = "1024195933443";
public static final String APP_BASE_URL = "https://www.eddycab.com/API/public/";
public static final String USER_REGISTER_URL = "api/users/register";
public static final String USER_LOGIN_URL = "api/users/login";
public static final String CREDIT_BONUS = "api/bananas/credit-and-bonus/";
public static final String GET_NEW_RESERVATIONS = "api/reservations/new-reservations-by-driver/";
public static final String USER_SET_BOOKING_URL = "api/reservations/set-booking";
public static final String USER_GET_DRIVER_LOCATION_POST_URL = "api/reservations/get-driver-location";
public static final String GET_BIDDING_RESULT_URL = "api/reservations/biddings/";
public static final String GET_USER_PROFILE = "api/users/get-user-profile/";
public static final String USER_RESERVATION_URL = "api/reservations/create";
public static final String API_RESERVATION_CANCEL = "api/reservations/cancel-reservation/";
public static final String USER_RESERVATION_RESPONSE_URL = "api/reservations/biddings/";
public static final String UPDATE_FAV_HOME_URL = "api/users/set-favourite-home-place";
public static final String UPDATE_FAV_WORK_URL = "api/users/set-favourite-work-place";
public static final String EDIT_PROFILE = "api/users/edit-profile";
public static final String UPDATE_USER_PROFILE_CHANGE_PROFILE_PIC = "api/users/set-profile-picture";
public static final String DRIVER_BID_POST = "api/reservations/driverPostBid";
public static final String DRIVER_RESERVATION_ASSIGN_POST = "api/reservations/reservation-assign";
public static final String DRIVER_RESERVATION_IGNORE_POST = "api/reservations/reservation-ignore";
public static final String DRIVER_RESERVATION_CANCEL_POST = "api/reservations/cancel-reservation-by-driver";
public static final String DRIVER_DRIVER_PICKUP_PASSENGER_POST = "api/reservations/pickup-passenger";
public static final String API_BIDDER_CANCEL_BY_USER = "api/reservations/cancel-bidder";
public static final String USER_TRIP_HISTORY_URL = "api/reservations/user-trip-history/";
public static final String Driver_BOOKING_HISTORY_URL = "api/reservations/driver-booking-history/";
public static final String NEAREST_DRIVER_BY_LOCATION = "api/reservations/get-nearest-drivers";
public static final String DRIVER_GET_QUITE_TIME_URL = "api/quietsetting/get-quiet-setting/";
public static final String DRIVER_SET_QUITE_TIME_URL = "api/quietsetting/set-quiet-setting";
public static final String SET_USER_LOCATION_POST_URL = "api/locations/set-location";
public static final String GET_PUSH_NOTIFICATION_DRIVER = "api/reservations/send-reservation-push-notifications/";
public static final String USER_FEEDBACK_RATING_URL = "api/feedbacks/user-feedback";
public static final String SUPPORT_FEEDBACK_POST_URL = "api/support/insert-feedback";
public static final String GET_MESSAGES_FROM_GCM_URL = "api/notifications/get-all-notifications/";
public static final String DRIVER_JOB_COMPLETE_URL = "api/reservations/reservation-complete";
public static final String DRIVER_PAY_VOUCHER_URL = "api/reservations/reservation-receipt";
public static final String DELETE_NOTIFICATION_MESSAGE = "api/notifications/remove-message/";
public static final String DELETE_ALL_NOTIFICATION_MESSAGE = "api/notifications/remove-user-messages/";
public static final String USER_LOGOUT = "api/users/logout/";
public static final String CHANGE_LANGUAGE = "api/users/set-language";
public static final String LANG_ENG = "English";
public static final String LANG_THAI = "Thai";
public static final String USER_TYPE_DRIVER = "driver";
public static final String USER_TYPE_USER = "user";
public static final String USER_TYPE_USER_ID = "1";
public static final String CALL_CENTER_NUMBER = "16600177711";
public static final String Driver_TYPE_USER_ID = "2";
public static final float CAMERA_FACTORY_UPDATE = 15;
public static final String DEVICE_TYPE = "android";
public static final long COUNTDOWN_TIME = 30000;
public static final String FORGET_PASSWORD_SEND_MESSAGE = "api/users/forget-password/";
public static final String CONFIRM_VERIFICATION_CODE = "api/users/verify-code";
public static final String API_SET_NEW_PASSWORD = "api/users/set-new-password";
public static final String RATE_USER_BY_DRIVER = "api/feedbacks/driver-feedback";
public static final String URL_RESEEND_CODE_GET = "api/users/resend-code/";
public static final String DRIVER_RECHARGE_HISTORY = "api/bananas/recharge-card-history/";
public static final String RECHARGE_CARD = "api/bananas/recharge-card/";
public static String DRIVER_JOB_COMPLETE_URL_call_center = "api/reservations/complete-callcenter-created-reservation";
}
最佳答案
您可以尝试以下方法:
创建您自己的自定义接口(interface)以获取回调:
import org.json.JSONObject;
/**
* Implement this listener for getting callback for UploadManager.doUpload()
* method.
*
*
*/
public interface UploadListener {
/**
* On success upload, server response for given network upload task.
*
* @param response
*/
void onSuccessUploadResponse(JSONObject response);
/**
* On failed upload, server response for given network upload task.
*
* @param error
*/
void onErrorUploadResponse(Throwable error);
}
然后为网络操作创建单例类并抽象出 volley 代码:
import org.json.JSONObject;
import android.content.Context;
import android.util.Log;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
/**
* Singleton class for network operations and to abstract out the volley
* code.
*
* @see <a
* href=" http://developer.android.com/training/volley/index.html">Volley</a>
*
*
*/
public class UploadManager {
private static UploadManager uploadManager;
private static RequestQueue mRequestQueue;
public static final int METHOD_POST = Request.Method.POST;
public static final int METHOD_GET = Request.Method.GET;
public static final int METHOD_PUT = Request.Method.PUT;
public static final int METHOD_DELETE = Request.Method.DELETE;
private UploadManager(Context context) {
mRequestQueue = Volley.newRequestQueue(context);
// Start the queue
mRequestQueue.start();
}
public static UploadManager getinstance(Context context) {
if (uploadManager == null) {
uploadManager = new UploadManager(context);
}
return uploadManager;
}
/**
* Implement UploadManager.CallbackListener for getting callback for this
* method.
*
* @param url
* : url to be used for network call.
* @param method
* : HTTP method type.
* @param jsonRequest
* : JSON object to be sent
* @param callbackListener
* : callback listener for getting success or error callback.
*/
public void doUpload(String url, int method, JSONObject jsonRequest,
final UploadListener callbackListener) {
JsonObjectRequest jsObjRequest = new JsonObjectRequest(method, url,
jsonRequest, new Response.Listener<JSONObject>() {
public void onResponse(JSONObject response) {
callbackListener.onSuccessUploadResponse(response);
};
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
if (null != error) {
callbackListener.onErrorUploadResponse(error
.getCause());
}
}
});
mRequestQueue.add(jsObjRequest);
}
}
并且您可以在所有类中使用 doUpload();
方法,而无需一次又一次地编写 volley 代码。
关于android - 需要 Volley API 调用简化解决方案,从单一方法调用所有 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37916686/
我想在单个 View 中显示文本、图像。也请帮助我 我想从我的类(class)而不是 xml 提供图像的 src 请帮助谢谢 最佳答案 你可以为此使用Button Button b=new Butto
我有一个消息模型,管理员和用户都可以创建消息。但对于管理员和用户来说,有单独的模型,称为管理员和用户。 消息模型有一个名为“created_by”的列,用于存储创建者的 ID。现在的问题是我如何与 M
我无法为菜单资源充气,并且无法将其附加到我的 Activity 的工具栏上。 这似乎很简单,但是我想我缺少明显的东西。我正在使用NavGraph,所以我不知道这是否影响工具栏? 有人看到我做错了吗?
我正在开发一个应用程序,它有一个 MainActivity 并且有许多用于医院、诊所的 ImageViews ... 当按下其中一个时,它会带你到一个新的 Activity DisplayActivi
我会尽量保持简短,但我需要一些建议。 我所在的团队正在并行开发适用于 android、iphone 和 wp7 的应用程序。我们有一个设计团队,可以为所有三个平台提出一个单一的设计。 最新应用程序的设
我正在使用 Josh Smith 中的示例.他有一个显示 CustomerViewModel 列表的 WorkspaceViewModel。他使用相同的 ViewModel 来显示所有客户和编辑一个客
我是 Azure 新手,正在尝试了解各种服务,目前我正在尝试了解移动服务及其各种功能,例如身份验证和推送。 移动服务是否仅支持一种操作系统上的一个应用程序,还是可以在多个操作系统(Android、iO
我在 Stoyan Stefanov 的书中读到了关于单一变量模式的内容。 JSLint 也很好。 但我在我的代码中注意到我可能会重载此模式。整个我的 .js 文件,整个脚本只是一个大变量。 例如:
我想在一个 View 中添加多个具有不同不透明度的阴影。阴影的规范如下: Y 偏移量为 4,模糊半径为 1 Y 偏移量为 10,模糊半径为 10 Y 偏移量为 2,模糊半径为 4 1 的模糊半径,1
我们有几个 API,我们希望通过客户端凭据流授予对客户端的访问权限。流程会像这样。 客户端根据某个范围从 is4 获取 token 客户端使用 token 访问第一个 API 客户端需要使用相同的 t
我是 ruby on rails 的新手。我正在尝试在 ruby on rails 上设计一个注册表单,该表单具有 Basic 和 Paid 用户的单选按钮。当用户点击付费并点击提交时,应该会
我用 Entity Framework 6 开发了一个项目,该项目使用 MySQL 作为数据库。在我的 Windows 系统上,该项目正在运行。现在我试图在我的 linux 机器上移动那个项目。为了运
我正在为我的电子商务应用程序创 build 计。它将拥有由 AWS Lambda 支持的多项服务。 Orderservice、InventoryService、PaymentService、Loggi
我目前正在开发一个执行以下操作的单 View 应用程序: 使用 CoreLocation 获取用户位置 将他们的经/纬度发送到 API 以 JSON 格式返回潮汐数据 深入研究 JSON 中的对象和键
我想托管各种 Angular2 应用程序,这些应用程序使用相同的框架包和来自根域和子域的 node_modules: domain.com subdomain.domain.com sub2.doma
我正在尝试使用 Git Publisher 插件来标记带有 $BUILD_TAG 的成功构建,但我无法找出它将接受的 Target remote name 的值。如果我在 GIT 配置中使用 Repo
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 8年前关闭。 Improve thi
有一个带有许多控件的 View (窗口),以简化: 此 View 用于显示和编辑多个配置: public class ViewModel: INotifyPropertyChanged
我有一个 textView 和类似的文本 “这是带有 KeyWord 和 Link 浏览的简单文本” 在上面的文字中我想制作.. 点击链接可打开该网址和点击该关键字在我的应用程序中打开一个新 Acti
我在我现有的应用程序中有一个任务,我们有 2 个不同的数据库,一个在 SQL Server 中,另一个在 Oracle 中,但是两个模式是相同的。 目前我们有一个使用 Entity Framework
我是一名优秀的程序员,十分优秀!