简体   繁体   English

Android Volley无法在单独的类中获得价值

[英]Android Volley can't get value in separate class

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;
    }
}

I wrote this code in a seperate class to prevent code repetition but when I run this inside a fragment like this: 我在一个单独的类中编写了此代码,以防止代码重复,但是当我在这样的片段中运行此代码时:

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();

And call the getValue() method. 并调用getValue()方法。 This method is always empty like: "". 此方法始终为空,例如:“”。 Does anyone know how I can enhance my class so this code will work? 有谁知道我如何增强我的课程,以便此代码起作用? This issue is not because of a bad link or bad request. 此问题不是由于链接错误或请求错误而引起的。 I can log the response and that does work (ofcourse inside VolleyStringRequest) 我可以记录响应并且可以正常工作(当然在VolleyStringRequest内部)

You run: 你跑:

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

But remember createStringRequest is async method and value is populated after some delay ae inside public void onResponse(String response) 但是请记住createStringRequest是异步方法,并且在public void onResponse(String response)内部延迟了一段时间后填充了value

So when you call volleyStringRequest.getValue(); 因此,当您调用volleyStringRequest.getValue(); you get empty string 你得到空字符串

To make it work you can write some interface as: 要使其工作,您可以编写一些接口,如下所示:

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

And pass it to VolleyStringRequest constructor: 并将其传递给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());

Next, change your VolleyStringRequest : 接下来,更改您的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= "";
    }
  //...
}

And once you got response from POST, call the callback as: 一旦您从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){}
}

So in bottom line instead to call volleyStringRequest.getValue(); 因此,在底线中改为调用volleyStringRequest.getValue();

you have: 你有:

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

that will be called when you get POST response 得到POST响应时将被调用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM