2017-09-02 5 views
0

le serveur renvoie la JSON suivante -aucun constructeur approprié trouvé pour erreur JsonObjectRequest lors de l'utilisation volée

[ { "user_id": 1, "prenom": "alex", "last_name": "goft", "mot de passe": "doom00", "email": "[email protected]", "nom d'utilisateur": "alexgoft" } ]

Je suis en essayant de le récupérer en utilisant volée la avec la fonction suivante -

public void makeRequest() { 
     final String HOST = "http://10.0.2.2:3000/"; 
     final String url = HOST + "get_data?type=user&username=alexgoft"; 
     RequestQueue queue = Volley.newRequestQueue(context); 

     Toast.makeText(context, url, Toast.LENGTH_LONG).show(); 

     // prepare the Request 
     JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET, url, 
       new Response.Listener<JSONArray>() { 
        @Override 
        public void onResponse(JSONArray response) { 
         Toast.makeText(context, "In succ", Toast.LENGTH_LONG).show(); 
         try { 
          returned_json = response.getJSONObject(0); 
         } catch (JSONException e) { 
          e.printStackTrace(); 
         } 
        } 
       }, 
       new Response.ErrorListener() { 
        @Override 
        public void onErrorResponse(VolleyError error) { 
         Toast.makeText(context, "In error", Toast.LENGTH_LONG).show(); 
         Toast.makeText(context, error.toString(), Toast.LENGTH_LONG).show(); 
         error.getStackTrace(); 
         Log.d("error", error.toString()); 
        } 
       }); 

     queue.add(getRequest); 
    } 

Le problème est que je reçois l'erreur suivante -

Error:(46, 40) error: no suitable constructor found for JsonObjectRequest(int,String,<anonymous Listener<JSONArray>>,<anonymous ErrorListener>) 
constructor JsonObjectRequest.JsonObjectRequest(int,String,Listener<JSONObject>,ErrorListener) is not applicable 
(argument mismatch; <anonymous Listener<JSONArray>> cannot be converted to Listener<JSONObject>) 
constructor JsonObjectRequest.JsonObjectRequest(String,JSONObject,Listener<JSONObject>,ErrorListener) is not applicable 
(argument mismatch; int cannot be converted to String) 

Que dois faire faire i pour que returned_json contiendra le JSON retourné de l'hôte?

Voici comment cela avait l'air ide -

enter image description here

+0

Attendez si vous avez des erreurs de compilation, alors pourquoi la réponse? –

Répondre

1

Vous devez passer null dans le cas où vous ne disposez pas de données à transmettre votre demande

Note: Étant donné que votre réponse est un JSONArray si vous avez besoin de mettre en œuvre JsonArrayRequest au lieu de JsonObjectRequest comme également souligné par @Krupa Kakkad

new JsonObjectRequest(Request.Method.GET, url, null, 
//           ^^^^ 
       new Response.Listener<JSONArray>() { 

From Docs

@param jsonRequest A {@link JSONObject} to post with the request. Null is allowed and 
    * indicates no parameters will be posted along with request. 

signature Constructor

JsonObjectRequest publique (méthode int, String url, JSONObject jsonRequest, auditeur Listener, ErrorListener ErrorListener) {

ou

JsonObjectRequest publique (String url, JSONObject jsonRequest, Listener listener, ErrorListener errorListener) {