2016-09-17 3 views
1

Когда я вызываю getData.It кажется, что получить результат из onResponse трудно. Я знаю, что он не может работать таким образом. Может ли кто-нибудь помочь мне решить эту проблему?Не выполнять Volley JSON onResponse function

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

что ответ, у получения от веб-сервиса. Можете ли вы добавить ответ 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); 
} 

Happy To Help.

Смежные вопросы