0

我需要在这里的 Jsonobject postbody 中的 postbody 中发布多个参数。我搜索了整个堆栈,但发现了。请帮助我

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

邮递员给出了这个回应在此处输入图像描述

4

2 回答 2

0

您可以使用 发送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> 
于 2017-11-15T09:59:12.063 回答
0

希望你正在寻找这个东西。!您可以在下面发送 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);
于 2017-11-15T10:05:45.487 回答