2016-08-08 1 views
1

Je travaille sur Volley mais je suis assez nouveau pour ce framework je n'ai aucune idée de comment publier des données JSON en utilisant volley, j'ai un exemple JSON s'il vous plaît dites-moi comment publier ces données JSON.Comment poster JSON en volley en utilisant android?

JSONObject jsonObject = new JSONObject(); 
jsonObject.put("email", "[email protected]"); 
jsonObject.put("password", "abcd123"); 
jsonObject.put("device", "jdghfdhgdhi"); 
jsonObject.put("latitude", 1.2456); 
jsonObject.put("longitude", 1.3466); 

S'il vous plaît veuillez passer par mon JSON et laissez-moi savoir comment publier ce JSON en utilisant volley.

+1

double possible de [Envoyer une requête POST avec des données JSON en utilisant Volley] (http://stackoverflow.com/questions/23220695/send-post-request-with-json-data-using-volley) – Sanoop

+0

http://stackoverflow.com/ques tions/23220695/send-post-request-with-json-data-using-volley Voir cette réponse –

+0

http://arnab.ch/blog/2013/08/asynchronous-http-requests-in-android-using- volley/passe par la volée. –

Répondre

0

essayez ceci:

StringRequest stringRequest = new StringRequest(Request.Method.POST, your url, 
        new Response.Listener<String>() { 
         @Override 
         public void onResponse(String response) { 
          try { 

          } catch (JSONException e) { 
           e.printStackTrace(); 
          } 
         } 
        }, 
        new Response.ErrorListener() { 
         @Override 
         public void onErrorResponse(VolleyError error) { 
          Toast.makeText(NoolReaderDashboard.this, error.toString(), Toast.LENGTH_LONG).show(); 
         } 
        }) { 
       @Override 
       protected Map<String, String> getParams() { 
        Map<String, String> params = new HashMap<String, String>(); 
      params .put("email", "[email protected]"); 
     params .put("password", "abcd123"); 
     params .put("device", "jdghfdhgdhi"); 
     params .put("latitude", 1.2456); 
     params .put("longitude", 1.3466); 
        return params; 
       } 
      }; 
      RequestQueue requestQueue = Volley.newRequestQueue(this); 
      requestQueue.add(stringRequest); 
+0

hey merci mais dans params .put ("latitude", 1.2456); params .put ("longitude", 1,3466); il affiche une erreur "Arguments erronés trouvés doubles" –

+1

params .put ("latitude", String.valueOf (1.2456)); et params .put ("longitude", String.valueOf (1.3466)); – onkar

+0

convertir en chaîne et le passer comme (1.2456+ "ou String.valueOf (1.2456));) @ AnshumanPattnaik – YUVRAJ

0

privé vide Postdata() {

String url = "http://www.example.com";        //url where you want to post data 
    StringRequest postRequest = new StringRequest(Request.Method.POST, url, 
      new Response.Listener<String>() { 
       @Override 
       public void onResponse(String response) { 
        try { 
         Log.i("response is","==>"+response); 

        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 
        Log.d("Response-------", "------>" + response); 
       } 
      }, 
      new Response.ErrorListener() { 
       public void onErrorResponse(VolleyError error) { 
        Log.e("Responce error==","===>"+error); 
        error.printStackTrace(); 
       } 
      } 
    ) { 

     protected Map<String, String> getParams() { 
      Map<String, String> params = new HashMap<>(); 
      // the POST parameters: 
      params.put("email", "[email protected]"); 
      params.put("password", "abcd123"); 
      params.put("latitude", 1.2456); 
      params.put("longitude", 1.3466); 
      Log.d("Value is ----------", ">" + params); 
      return params; 
     } 
    }; 
    MyApplication .getInstance().addToRequestQueue(postRequest); 
} 
0

Essayez ce code

JSONObject jsonObject = new JSONObject(); 
       try { 
     jsonObject.put("email", "[email protected]"); 
     jsonObject.put("password", "abcd123"); 
     jsonObject.put("device", "jdghfdhgdhi"); 
     jsonObject.put("latitude", 1.2456); 
     jsonObject.put("longitude", 1.3466); 

       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 


     final JsonObjectRequest postRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() { 
       @Override 
       public void onResponse(JSONObject response) { 


// here parse your response json data 

        try { 
         String status = response.getString("XSTS"); 


        } catch (JSONException e) { 
         Toast.makeText(this, "Something went wrong", Toast.LENGTH_SHORT).show(); 
         e.printStackTrace(); 
        } 

       } 
      }, new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        // Handle your error types accordingly.For Timeout & No connection error, you can show 'retry' button. 
        // For AuthFailure, you can re login with user credentials. 
        // In this case you can check how client is forming the api and debug accordingly. 
        // For ServerError 5xx, you can do retry or handle accordingly. 
        if (error instanceof NetworkError) { 
         Toast.makeText(LoginScreenActivity.this, "No internet connection", Toast.LENGTH_SHORT).show(); 
        } else if (error instanceof ServerError) { 
         Toast.makeText(LoginScreenActivity.this, "Our server is busy please try again later", Toast.LENGTH_SHORT).show(); 
        } else if (error instanceof AuthFailureError) { 
         Toast.makeText(LoginScreenActivity.this, "AuthFailure Error please try again later", Toast.LENGTH_SHORT).show(); 
        } else if (error instanceof ParseError) { 
         Toast.makeText(LoginScreenActivity.this, "Parse Error please try again later", Toast.LENGTH_SHORT).show(); 
        } else if (error instanceof NoConnectionError) { 
         Toast.makeText(LoginScreenActivity.this, "No connection", Toast.LENGTH_SHORT).show(); 
        } else if (error instanceof TimeoutError) { 
         Toast.makeText(LoginScreenActivity.this, "Server time out please try again later", Toast.LENGTH_SHORT).show(); 
        } 
        error.printStackTrace(); 
        progressDialog.dismiss(); 
       } 
      }) { 
       @Override 
       public Map<String, String> getHeaders() throws AuthFailureError { 
        HashMap<String, String> headers = new HashMap<String, String>(); 
        headers.put("Content-Type", "application/json"); 

        return headers; 
       } 
      }; 
      Volley.newRequestQueue(LoginScreenActivity.this).add(postRequest);