您可以使用 发送JsonObject
请求Volley library
。请参阅下面的代码,希望这会对您有所帮助。
try {
JSONObject jsonObject = new JSONObject("{" +
"\"sessionId\":\"" + sessionId + "\"," +
"\"hasAudio\":\"" + hasAudio + "\"," +
"\"hasVideo\":\"" + hasVideo + "\"," +
"\"outputMode\":\"" + outputMode + "\"}");
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(YOUR_URL , jsonObject, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Utils.hideProgressDialog();
try{
if(response!=null){
// do something with the response
}
}catch (JSONException ex){
ex.printStackTrace();
Toast.makeText(getActivity(), "Ops, Something wrong. Try again later. ", Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Handle Error .
}
})
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Content-Type", "application/json");
return params;
} ;
AppController.getInstance().addToRequestQueue(jsonObjectRequest, "json_object");
} catch (Exception ex) {
ex.printStackTrace();
Toast.makeText(getContext(), "Ops, Something wrong. Try again later. ", Toast.LENGTH_SHORT).show();
}
添加 AppController 类如下:
public class AppController extends Application {
public static final String TAG = AppController.class.getSimpleName();
private RequestQueue mRequestQueue;
private static AppController mInstance;
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
}
public static synchronized AppController getInstance() {
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
mRequestQueue = Volley.newRequestQueue(getApplicationContext());
}
return mRequestQueue;
}
public <T> void addToRequestQueue(Request<T> req, String tag) {
req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
getRequestQueue().add(req);
}
public <T> void addToRequestQueue(Request<T> req) {
req.setTag(TAG);
getRequestQueue().add(req);
}
public void cancelPendingRequests(Object tag) {
if (mRequestQueue != null) {
mRequestQueue.cancelAll(tag);
}
}
}
在标签中添加AppContoller
您的Manifest.xml
文件。<application>
<application
android:name=".AppController"> ...... </application>