2016-09-17 58 views
1

当我调用getData.It似乎很难从onResponse中获取结果。我知道它不能以目前的方式工作。有谁能帮我解决这个问题吗?不执行Volley JSON onResponse函数

的getData()

private void getData(){ 

    //Creating a string request 
    StringRequest stringRequest = new StringRequest(SPINNER_URL, new Response.Listener<String>() { 
     @Override 
     public void onResponse(String response) { 
      // Log.d("Country_name","hi"); 
      JSONObject j = null; 
      try { 
       //Parsing the fetched Json String to JSON Object 
       j = new JSONObject(response); 

       //Storing the Array of JSON String to our JSON Array 
       result = j.getJSONArray(JSON_ARRAY); 

       Log.v("xxxxx",result.toString()); 
       String mysh=result.toString().substring(1, result.toString().length()-1); 


       JSONArray jsonArray = new JSONArray(mysh); 
       //Calling method getCountry to get the country from the JSON Array 
       getCountry(jsonArray); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
     } 
    }, 
      new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 

       } 
      }); 

    //Creating a request queue 
    RequestQueue requestQueue = Volley.newRequestQueue(this); 

    //Adding request to the queue 
    requestQueue.add(stringRequest); 
} 
+0

什么响应,U从web服务获得。你可以添加json响应吗? – W4R10CK

+0

onErrorResponse()怎么样?是不是因为一些错误而被称为? – abbath

+1

也许如果你做一个jsonRequest而不是一个StringRequest,它会更容易?另外,为什么试图抓住一切?如果回应是好的,那么应该没有必要,对吧? –

回答

1

试试这个,我认为它应该工作

private void getData(){ 

//Creating a string request 
StringRequest stringRequest = new StringRequest(SPINNER_URL, new Response.Listener<String>() { 
    @Override 
    public void onResponse(String response) { 


     try { 

      JSONArray jsonArray = new JSONArray(response); 
      getCountry(jsonArray); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
    } 
}, 
     new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 

      } 
     }); 


RequestQueue requestQueue = Volley.newRequestQueue(this); 


requestQueue.add(stringRequest); 
} 
0

尝试的是,这将有助于。

private void getData() { 

    String tag_string_req = "req_name"; 
    spotsDialog.show(); 

    StringRequest strReq = new StringRequest(Method.POST, 
      YOUR URL, new Response.Listener<String>() { 

     @Override 
     public void onResponse(String response) { 
      Log.d(TAG, "Response: " + response.toString()); 

      try { 
       JSONObject object = new JSONObject(response); 
       JSONArray array = object.getJSONArray("YOUR ARRAY NAME"); 
       for (int i=0;i<array.length();i++){ 
        String result = array.getString(i).toString(); 
       } 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 

     } 
    }, new Response.ErrorListener() { 

     @Override 
     public void onErrorResponse(VolleyError error) { 
      Log.e(TAG, "Error: " + error.getMessage()); 

     } 
    }); 
    strReq.setRetryPolicy(new RetryPolicy() { 

     @Override 
     public void retry(VolleyError arg0) throws VolleyError { 
     } 

     @Override 
     public int getCurrentTimeout() { 
      return 0; 
     } 

     @Override 
     public int getCurrentRetryCount() { 
      return 0; 
     } 
    }); 
    RequestQueue requestQueue = Volley.newRequestQueue(this); 

//Adding request to the queue 
requestQueue.add(stringRequest); 
} 

高兴地帮助。