簡體   English   中英

如何使用 Volley 將標頭和正文添加到 REST 請求中?

[英]How do I add header and body into REST request using Volley?

以前,我向https://www.thecocktaildb.com/api/json/v1/1/search.php?i=beer發出請求,這是一個不需要任何標題和正文的公共 API,但現在我需要向https://api.candy.mn/resource/partner/v1/sell發送請求 (POST),它需要頭中的 Authorization 和 Content-Type 以及正文中的一些參數。 我不知道如何將它們添加到我的代碼中:

package com.example.john.candyapi;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends AppCompatActivity {
    private TextView mTextViewResult;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mTextViewResult = findViewById(R.id.text_view_result);

    String URL = "https://www.thecocktaildb.com/api/json/v1/1/search.php?i=beer";
    String in = "ingridients";

    //creating requestQueue object by calling newRequestQueue on Volley class
    RequestQueue requestQueue = Volley.newRequestQueue(this);

    //creating json obeject request
    JsonObjectRequest objectRequest = new JsonObjectRequest(
            Request.Method.POST,
            URL,
            null,
            new Response.Listener<JSONObject>() {
                @Override
                //if response is successful
                public void onResponse(JSONObject response) {
                        try{
                            //get from strDescription of 0th row of ingridients
                            JSONArray jsonarray = response.getJSONArray("ingredients");
                            JSONObject one = jsonarray.getJSONObject(0);
                            String strDescr = one.getString("strDescription");
                            //append this String to TextView
                            mTextViewResult.append(strDescr);
                        }catch (JSONException e){
                            e.printStackTrace();
                        }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                        Log.e("Rest Response", error.toString());
                }
            }
    );

    //requesting created request
    requestQueue.add(objectRequest);
}


}
  //setting up the parameters
  JSONObject parms = new JSONObject();
  parms.put("parm1", "value");


  //creating json obeject request
    JsonObjectRequest objectRequest = new JsonObjectRequest(
            Request.Method.POST,
            URL,
            parms,
            new Response.Listener<JSONObject>() {
                @Override
                //if response is successful
                public void onResponse(JSONObject response) {
                        ......
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                        Log.e("Rest Response", error.toString());
                }
            }
    ) {     
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError { 
                Map<String, String>  params = new HashMap<String, String>();  
                params.put("Content-Type", "application/json");  

                return params;  
        }
    };

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM