【问题标题】:Manage types of responses from the server管理来自服务器的响应类型
【发布时间】:2019-05-02 06:46:38
【问题描述】:

我很困惑,希望你能帮助我解决这个疑问,我正在与 Volley 库一起使用 POST 方法,服务器给我带来了两种类型的答案。

第一个

如果用户正确输入了他的数据,服务器必须把用户的数据扔给我。如下所示。

Result of the user's request.

这就是请求从代码发送到服务器的方式。

 public void getData() {

    StringRequest postRequest = new StringRequest(Request.Method.POST, urlLogin,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {

                        JSONObject jsonResponse = new JSONObject(response);
                        System.out.print(jsonResponse);



                    } catch (JSONException e) {

                        e.printStackTrace();
                        Log.d(TAG, String.valueOf(e));
                        Toast.makeText(Login.this,e.toString(),Toast.LENGTH_LONG).show();


                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    error.printStackTrace();
                    Log.d(TAG, String.valueOf(error));
                    Toast.makeText(Login.this,error.toString(),Toast.LENGTH_LONG).show();

                }
            }
    ) {

        @Override
        public String getBodyContentType() {
            return "application/x-www-form-urlencoded; charset=UTF-8";
        }

        // here is params will add to your url using post method
        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<String, String>();
            params.put("Email", edtEmail.getText().toString());
            params.put("Password", edtPassword.getText().toString());
            return params;
        }
    };

    postRequest.setRetryPolicy(new DefaultRetryPolicy(
            10000,
            DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(postRequest);
    DiskBasedCache cache = new DiskBasedCache(this.getCacheDir(), 500 * 1024 * 1024);
    requestQueue = new RequestQueue(cache, new BasicNetwork(new HurlStack()));
    requestQueue.start();
}

到目前为止一切正常,当您输入一些错误的数据时会出现问题,因为服务器没有回答任何过时的问题。

信息。它应该显示用户是否输入了一些错误信息。

Result when entering erroneous information

我遇到的问题是,当您发送这些或其他错误数据时,服务器不会以任何类型的数据响应我,它只会直接响应 Response.ErrorListener ()

new Response.ErrorListener() { //com.android.volley.ServerError
                @Override
                public void onErrorResponse(VolleyError error) {
                    error.printStackTrace();
                    Log.d(TAG, String.valueOf(error));
                    Toast.makeText(Login.this,error.toString(),Toast.LENGTH_LONG).show();

                }
            }

通过该错误的调试后,“应用程序正在运行”消息继续,提前谢谢你,我希望你能帮助我解决错误。

【问题讨论】:

    标签: json android-studio http server android-volley


    【解决方案1】:

    您需要设计 Volley 的基础架构。 就像您需要为向服务器发出的每个请求创建一个基类

    abstract class BaseJSONRequest<T> extends Request<T> {
    

    在这个 BaseJSONRequest 中,你需要重写 Volley Request 类的方法

    @Override
    protected Response<T> parseNetworkResponse(NetworkResponse networkResponse) {
    and this below
    @Override
    public void deliverError(VolleyError error) {
    

    那么你需要为你向服务器发出的每个请求扩展这个 BaseJSONRequest 下面我举一个例子。

    public class LoginRequest extends BaseJSONRequest<User> {
    
    private final String mUserNamePassword;
    private final String mpassword;
    
    public LoginRequest(String userName, String password, boolean byPin, String deviceId, Callback<User> callback) {
        super(Method.GET, NetworkController.URL_LOGIN, createParams(byPin, deviceId), callback);
    }
    
    private static HashMap<String, String> createParams(boolean byPin, String deviceId) {
        HashMap<String, String> map = new HashMap<>();
        return map;
    }
    
    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
    
    }
    
    
    @Override
    protected User parseData(JSONObject jsonObject) {
        try {
    
            System.out.println("-Login Request--"+jsonObject);
    
            return new User(jsonObject.getJSONObject("user"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-26
      • 2018-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多