【问题标题】:posting multiple parameters in jsonobjectrequest volley在 jsonobjectrequest volley 中发布多个参数
【发布时间】:2018-04-28 11:08:26
【问题描述】:

我需要在此处的 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

邮递员回复

【问题讨论】:

    标签: android opentok jsonobjectrequest


    【解决方案1】:

    您可以使用Volley library 发送JsonObject 请求。请参阅下面的代码,希望这会对您有所帮助。

         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);
            }
        }
    
    
    
    }
    

    Manifest.xml 文件的&lt;application&gt; 标记中添加AppContoller

     <application
            android:name=".AppController">  ...... </application> 
    

    【讨论】:

    • 请发给我 AppController 类,因为我没有在我的代码中使用它
    • 编辑了我的答案。让我知道它是否有效或您遇到任何问题。
    • 您能否使用 Postman 或 ARC 提供您请求的快照。并调试您发送的对象值。
    • 我需要将它包含在我的代码“Content-Type:application/json”中。也许这就是我收到该错误的原因。你能帮帮我吗?
    • 邮递员给出这个响应 { "code": -1, "message": "No suitable authentication found" }
    【解决方案2】:

    希望你正在寻找这个东西。! 您可以在method下方发送JSON Object请求尝试。

    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);
    

    【讨论】:

    • 在哪里添加requestCall?
    • Volley.newRequestQueue(context).add(requestCall);
    • 请发布您的整个代码。?
    • 我发现这是由于服务器的身份验证问题。需要 JWT 令牌。无论如何谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-09
    • 1970-01-01
    • 2021-06-21
    • 1970-01-01
    相关资源
    最近更新 更多