2017-07-02 25 views
0
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; 
    } 
} 

我在一個單獨的類寫了這個代碼,以防止重複的代碼,但是當我運行這個像這樣的片段中:Android的凌空不能在單獨的類中獲得價值

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()方法。此方法始終爲空:「」。有誰知道我可以如何提高我的課程,這樣的代碼將工作?這個問題並不是因爲錯誤的鏈接或錯誤的請求。我可以登錄響應,並且不工作(ofcourse內VolleyStringRequest)

回答

1

您運行:

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

但要記住createStringRequest是異步方法和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迴應時調用

+0

再次感謝您的問題。我如何將onResponse的resp分配給一個可變的值?我似乎無法得到它的工作 –

+0

@SanderBakker你在用'volleyStringRequest.getValue();'做什麼? –

+0

分配變量?這將工作在onCreateView? –