【问题标题】:Posting json object, waiting response in String Volley发布 json 对象,在 String Volley 中等待响应
【发布时间】:2017-03-23 12:20:25
【问题描述】:

我一直在使用 volley 库在我的 android 应用中使用服务。我注意到一些奇怪的事情,当我尝试将 json 对象发布到服务器时,我必须创建一个 JSONObject 类型的响应侦听器。但我的服务器返回一个字符串,成功或失败。是否可以编写一个等待字符串响应的 JsonObjectRequest?这就是我所写的。服务正常运行,但是由于无法将String收敛到JSON,在post成功后触发onErrorResponse

            JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.POST, url, jsonBody,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {

                        Toast.makeText(StartingActivity.this,response.toString(),Toast.LENGTH_SHORT).show();

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

                error.printStackTrace();
                Toast.makeText(StartingActivity.this,"That didn't work",Toast.LENGTH_SHORT).show();

            }
        });


        RequestQueue queue = Volley.newRequestQueue(this);
        queue.add(jsonRequest);

【问题讨论】:

    标签: android json android-volley


    【解决方案1】:

    使用 StringRequest 代替 JsonObjectRequest。它将以字符串形式返回响应,如下所示:

     StringRequest stringRequest = new StringRequest(Request.Method.POST,url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                                     Log.e(TAG,response); 
                               },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        error.printStackTrace();
                    }
                }) {
            @Override
            public byte[] getBody() throws AuthFailureError {
                //use this to post data.
            }
    
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                //use this for headers.
            }
    
        };
    
    stringRequest.setRetryPolicy(new RetryPolicy() {
            @Override
            public int getCurrentTimeout() {
                return 50000;
            }
    
            @Override
            public int getCurrentRetryCount() {
                return 50000;
            }
    
            @Override
            public void retry(VolleyError error) throws VolleyError {
    
            }
        });
    
        RequestQueue requestQueue = Volley.newRequestQueue(context);
        requestQueue.add(stringRequest);
    

    【讨论】:

      【解决方案2】:

      您发送到服务器的 jsonBody 应该是字符串。

        JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.POST, url, jsonBody.toString(),
      

      我希望这会对你有所帮助。谢谢!

      【讨论】:

      • 这不是问题,也不是问题。我问我如何才能改变我收到的回复类型
      猜你喜欢
      • 2022-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多