这是使用Volley发送带有JSON正文的POST请求的正确方法吗?

时间:2019-02-13 07:42:24

标签: android json post android-volley

我发送的Post请求没有任何响应。

我有这个登录API,发布请求,其中在正文中将参数设置为行,并且标头参数为{“ content-type”:“ application / json”}。 我使用Post man测试了API,并且可以正常工作。 Post man request

以下是我发送请求的代码:

private void SignIn(){

    String url = WebConfiguration.getServer() + WebServiceParams.LOG_IN;
    Map<String, String> params = WebServiceParams.getLogInParams(mEmail, mPassword);
    JSONObject jsonParams = new JSONObject( params);
    // json request
    DataLoader.loadJsonDataPostWithProgress3(this, url,
    new SignInRequestHandler(), mProgress, jsonParams , Request.Priority.HIGH, TAG);
    }

//

public static void loadJsonDataPostWithProgress3(final Activity activity, String url, final OnJsonDataLoadedListener listener,
                                            final ProgressDialog progress, final JSONObject params,
                                            final Request.Priority priority, String tag) {
Utils.showProgress(progress);

    JsonObjectRequest jsonObjReq = new JsonObjectRequest(url, params,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {

                    JSONObject jsonResponse = null;
                    try{
                        jsonResponse = (JSONObject)response;
                    }catch(Exception ex){
                        ex.printStackTrace();
                    }
                    Utils.dismissProgress(progress);
                    if (listener != null) {
                        LogInResponse webServiceLoginResponse = JsonParser.json2WebServiceLogInResponse(jsonResponse);
                        if (webServiceLoginResponse != null) {
                            if (webServiceLoginResponse.getMsg() == "error") {
                                listener.onJsonDataLoadedWithError(MyApplication.ERROR_CODE, webServiceLoginResponse.getDetails());
                            } else if (webServiceLoginResponse.getMsg() == "ok") {
                                listener.onJsonDataLoadedSuccessfully(webServiceLoginResponse.getUserData());
                            }
                        } else {
                            Utils.showDialog(activity, R.string.error_parse);
                        }
                    }
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Utils.dismissProgress(progress);
            if (listener != null) {
                int errorId = R.string.error_connection;
                if (error instanceof TimeoutError || error instanceof NoConnectionError) {
                    errorId = R.string.error_connection;
                } else if (error instanceof AuthFailureError) {
                    errorId = R.string.error_connection;
                } else if (error instanceof ServerError) {
                    errorId = R.string.error_server;
                } else if (error instanceof NetworkError) {
                    errorId = R.string.error_connection;
                } else if (error instanceof ParseError) {
                    errorId = R.string.error_parse;
                }
                listener.onJsonDataLoadingFailure(errorId);
            }
        }
    }) {
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> params = new HashMap<String, String>();
            params.put("Content-Type", "application/json");
            return params;
        }
    };
jsonObjReq.setRetryPolicy(new DefaultRetryPolicy(
        TIMEOUT_MS,
        NUM_RETRIES,
        DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
VolleySingleton.getInstance().addToRequestQueue(jsonObjReq, tag);

}

///// 私有类SignInRequestHandler实现了DataLoader.OnJsonDataLoadedListener {

@Override
public void onJsonDataLoadedSuccessfully(JSONObject userData) {
    onLoginSucceeded(JsonParser.json2UserData(userData));
}

@Override
public void onJsonDataLoadedWithError(int errorCode, String errorMessage) {
    Utils.showDialog(SignInActivity.this, errorMessage);
}

@Override
public void onJsonDataLoadingFailure(int errorId) {
    Utils.showDialog(SignInActivity.this, errorId);
}

}

当我调试代码时,它将传递给“ SignIn()”函数,但在请求时 “ DataLoader.loadJsonDataPostWithProgress3”它只是将其传递(因为不存在),它不会传递给处理程序,无论是错误还是处理失败。 在应用程序端,我按登录以显示进度,然后进度停止,没有任何消息。

0 个答案:

没有答案
相关问题