我需要在Jsonobject postbody中在postbody中发布多个参数。我搜遍了整个堆栈,但是cudnt找出来了。请帮助我
public void startArchive(String sessionId) {
JSONObject postBody = null;
try {
postBody = new JSONObject("{\"sessionId\": \"" + sessionId + "\"}");
} catch (JSONException e){
Log.e(LOG_TAG, "Parsing json body failed");
e.getStackTrace();
}
this.reqQueue.add(new JsonObjectRequest(Request.Method.POST, OpenTokConfig.ARCHIVE_START_ENDPOINT,
postBody,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.i(LOG_TAG, "archive started");
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
delegate.onWebServiceCoordinatorError(error);
}
}));
}
我的参数
{
"sessionId" : "session_id",
"hasAudio" : true,
"hasVideo" : true,
"name" : "archive_name"
"outputMode" : "composed",
}
我还需要包含这个
Content-Type:application/json
答案 0 :(得分:1)
您可以使用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>
答案 1 :(得分:1)
希望你正在寻找这个东西。
您可以发送JSON Object
请求尝试method
以下。
HashMap<String, String> parrams = new HashMap<String, String>();
parrams.put("sessionId", "session_id");
parrams.put("hasAudio", "true");
parrams.put("hasVideo", "true");
parrams.put("name", "archive_name");
parrams.put("outputMode", "composed");
JsonObjectRequest requestCall = new JsonObjectRequest(Request.Method.POST, OpenTokConfig.ARCHIVE_START_ENDPOINT,
new JSONObject(parrams) ,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.i(LOG_TAG, "archive started");
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
delegate.onWebServiceCoordinatorError(error);
}
}));
Volley.newRequestQueue(context).add(requestCall);