2016-08-05 6 views
0

Ich benutze volley-bibliothek für die anfrage. Ich muss Param als JSON-Array veröffentlichen, weil ich es auf der anderen Seite als JSON-Array empfange. Wie kann ich meine Parameter in JSON Array konvertieren? hier ist mein Codeposten params als json in android volley

public void SendData() {{ 
    final StringRequest strReq = new StringRequest(Request.Method.POST, GET_STUDENTS_BY_ID, new Response.Listener<String>() { 

      @Override 
     public void onResponse(String response) { 


       Log.v("failedd",response); 
     } 

    }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 
      Log.e("Error", "Registration Error: " + error.getMessage()); 

     } 
    }) 

    { 
     @Override 
     protected Map<String, String> getParams() { 
      // Posting params to register url 


      Map<String, String> params = new HashMap<String, String>(); 

      params.put("user_id", user_id); 

      params.put("student_id", studId); 
      params.put("to", tomail); 
      params.put("subject", subjects); 
      params.put("description", descriptions); 
      return params; 

     } 

     @Override 
     public Map<String, String> getHeaders() throws AuthFailureError { 
      HashMap<String, String> headers = new HashMap<String, String>(); 
      headers.put("Content-Type", "application/json; charset=utf-8"); 
      return headers; 
     } 
    }; 

    AppController.getInstance().addToRequestQueue(strReq, tag_json_obj); 

} 

bitte helfen ..

Antwort

1
public void SendData() { 

    Map<String, String> params = new HashMap<String, String>(); 

     params.put("user_id", user_id); 
     params.put("student_id", studId); 
     params.put("to", tomail); 
     params.put("subject", subjects); 
     params.put("description", descriptions); 

    JSONObject parameters = new JSONObject(params); 

    JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,GET_STUDENTS_BY_ID,parameters,new Response.Listener<JSONObject>() { 

      @Override 
      public void onResponse(JSONObject response) { 
       Log.d(TAG, response.toString()); 
      } 
     }, new Response.ErrorListener() { 

      @Override 
      public void onErrorResponse(VolleyError error) { 
       VolleyLog.d(TAG, "Error: " + error.getMessage()); 
      } 
     }) { 

    @Override 
    public Map<String, String> getHeaders() throws AuthFailureError { 
     HashMap<String, String> headers = new HashMap<String, String>(); 
     headers.put("Content-Type", "application/json; charset=utf-8"); 
     return headers; 
    } 

}; 

    AppController.getInstance().addToRequestQueue(jsonObjReq, tag_json_obj); 
} 
+0

Welche Bibliothek Sie mcxiaoke Volley oder Googles neue Volley verwendet ?? – Nikhil

+0

Googles neuer Volley – Bivin

+0

Es gibt Syntaxfehler ... .Sorry für das jetzt versuchen Sie bitte 'neues JSONObject (params)' anstelle von 'neuem JsonObject (params)'. – Nikhil