public class VolleyStringRequest {
    String url;
    String body;
    String value;
    public VolleyStringRequest(String url, String body){
        this.url = url;
        this.body = body;
        value= "";
    }
    public StringRequest createStringRequest(){
        StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        // Do something with the response
                        Log.e("Response", response);
                        try{
                            JSONObject o = new JSONObject(response);
                            JSONArray values=o.getJSONArray("response");
                            value += values.toString();
                        }  catch (JSONException ex){}

                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        // Handle error
                    }
                }) {
            @Override
            public byte[] getBody() throws AuthFailureError {
                return body.getBytes();
            };
            @Override
            public String getBodyContentType() {
                return "application/json";
            }
        };
        return stringRequest;
    }

    public String getValue() {
        return value;
    }
}


我在一个单独的类中编写了此代码,以防止代码重复,但是当我在这样的片段中运行此代码时:

RequestQueue rq = Volley.newRequestQueue(getActivity().getApplicationContext());
        String url= "http://grwn.ddns.net:1337/results";
        final String body = "{\"id\":1}";
        VolleyStringRequest volleyStringRequest = new VolleyStringRequest(url, body);
        rq.add(volleyStringRequest.createStringRequest());
        volleyStringRequest.getValue();


并调用getValue()方法。此方法始终为空,例如:“”。有谁知道我可以如何增强我的课程,以便此代码起作用?此问题不是由于链接错误或请求错误而引起的。我可以记录响应,并且可以正常工作(当然在VolleyStringRequest内部)

最佳答案

你跑:

VolleyStringRequest volleyStringRequest = new VolleyStringRequest(url, body);
rq.add(volleyStringRequest.createStringRequest());
volleyStringRequest.getValue();


但是请记住createStringRequest是一种异步方法,并且在某些延迟a.e之后填充value。内public void onResponse(String response)

因此,当您呼叫volleyStringRequest.getValue();时,您会得到一个空字符串

要使其工作,您可以编写一些接口,如下所示:

  public interface RequestHandlerInterface(){
    void onResponse(String resp);
  }


并将其传递给VolleyStringRequest构造函数:

    RequestHandlerInterface rh = this;  //Your main class should implement this method
    RequestQueue rq = Volley.newRequestQueue(getActivity().getApplicationContext());
    String url= "http://grwn.ddns.net:1337/results";
    final String body = "{\"id\":1}";
    VolleyStringRequest volleyStringRequest = new VolleyStringRequest(url, body, rh);
    rq.add(volleyStringRequest.createStringRequest());


接下来,更改您的VolleyStringRequest

public class VolleyStringRequest {
    String url;
    String body;
    String value;
    public VolleyStringRequest(String url, String body, RequestHandlerInterface rh){
        this.url = url;
        this.body = body;
        this.rh = rh;
        value= "";
    }
  //...
}


当您收到POST的回复后,将回调调用为:

  @Override
 public void onResponse(String response) {
     // Do something with the response
       Log.e("Response", response);
         try{
          JSONObject o = new JSONObject(response);
          JSONArray values=o.getJSONArray("response");
          value += values.toString();
           if(this.rh != null){
             this.rh.onResponse(value);
           }
          }  catch (JSONException ex){}
}


所以在底线而不是调用volleyStringRequest.getValue();

你有:

@Override
void onResponse(String resp){
     // here you go
 }


得到POST响应时将被调用

07-24 21:19