第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

帶有標(biāo)頭和正文參數(shù)的排球請(qǐng)求

帶有標(biāo)頭和正文參數(shù)的排球請(qǐng)求

HUWWW 2022-06-04 15:37:14
我需要提出一個(gè) api 請(qǐng)求。兩個(gè)標(biāo)題:接受授權(quán)五個(gè)身體參數(shù)。數(shù)字制作模型描述盤(pán)子通過(guò)郵遞員,一切都很好。但是當(dāng)我嘗試通過(guò)android應(yīng)用程序時(shí),我無(wú)法通過(guò)。注意:通過(guò)同一主機(jī)登錄效果很好,所以設(shè)置不是問(wèn)題,我認(rèn)為我的主要問(wèn)題在于 api 調(diào)用。public void add(View view) {        RequestQueue queue = Volley.newRequestQueue(this);        String URL = "http://10.0.2.2:8000/api/trucks";        StringRequest request = new StringRequest(Request.Method.POST, URL,                new Response.Listener<String>() {                    @Override                    public void onResponse(String response) {                        try {                            JSONObject jsonObj = new JSONObject(response);                            // parse response                        } catch (JSONException e) {                            e.printStackTrace();                        }                    }                },                new Response.ErrorListener() {                    @Override                    public void onErrorResponse(VolleyError error) {                        NetworkResponse response = error.networkResponse;                        String errorMsg = "";                        if (response != null && response.data != null) {                            String errorString = new String(response.data);                        }                    }                }        ) {            @Override            public Map<String, String> getHeaders() {                HashMap<String, String> headers = new HashMap<>();                headers.put("Accept", "application/json");                headers.put("Authorization", "Bearer " + myToken);                return headers;            }        };        queue.add(request);    }
查看完整描述

3 回答

?
MMTTMM

TA貢獻(xiàn)1869條經(jīng)驗(yàn) 獲得超4個(gè)贊

通過(guò)解決方案#1。


public void add(View view) throws JSONException {

    RequestQueue queue = Volley.newRequestQueue(this);

    TextInputEditText number = findViewById(R.id.textInputEditTextNumber);

    TextInputEditText make = findViewById(R.id.textInputEditTextMake);

    TextInputEditText model = findViewById(R.id.textInputEditTextModel);

    TextInputEditText description = findViewById(R.id.textInputEditTextDescription);

    TextInputEditText plates = findViewById(R.id.textInputEditTextPlates);


    JSONObject jsonObject = new JSONObject();

    jsonObject.put("Number", number.getText().toString());

    jsonObject.put("Make", make.getText().toString());

    jsonObject.put("Model", model.getText().toString());

    jsonObject.put("Description", description.getText().toString());

    jsonObject.put("Plates", plates.getText().toString());

    final String requestBody = jsonObject.toString();


    JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.POST, "http://10.0.2.2:8000/api/trucks", jsonObject,

            new Response.Listener<JSONObject>() {

        @Override

        public void onResponse(JSONObject response) {


            //now handle the response

            Toast.makeText(truck_add.this, response.toString(), Toast.LENGTH_SHORT).show();


        }

    }, new Response.ErrorListener() {

        @Override

        public void onErrorResponse(VolleyError error) {


            //handle the error

            Toast.makeText(truck_add.this, "An error occurred", Toast.LENGTH_SHORT).show();

            error.printStackTrace();

        }

    }) {    //this is the part, that adds the header to the request

        @Override

        public Map<String, String> getHeaders() {

            Map<String, String> params = new HashMap<String, String>();

            params.put("Accept", "application/json");

            params.put("Authorization", myToken);

            return params;

        }

    };

    queue.add(jsonRequest);

}


查看完整回答
反對(duì) 回復(fù) 2022-06-04
?
qq_遁去的一_1

TA貢獻(xiàn)1725條經(jīng)驗(yàn) 獲得超8個(gè)贊

當(dāng)你想通過(guò) body 傳遞數(shù)據(jù)時(shí),你需要在字符串請(qǐng)求之前創(chuàng)建一個(gè) json 對(duì)象。

試試這種方式,

1. 為請(qǐng)求創(chuàng)建一個(gè)字符串 url。

2.為body數(shù)據(jù)創(chuàng)建json對(duì)象并將數(shù)據(jù)傳遞給它。喜歡,


JSONObject jsonObject= new JSONObject();  

jsonObject.put("Number", address.getNumber());  

jsonObject.put("Make", address.getMake());  

jsonObject.put("Model", address.getModel());  

jsonObject.put("Description", address.getDescription());  

jsonObject.put("Plates", address.getPlates());  

final String requestBody=jsonObject.toString();  

3. 在此之后,應(yīng)用 stringRequest。

4. 現(xiàn)在,在標(biāo)題方法之前和錯(cuò)誤方法之后添加以下行。


@Override

        public String getBodyContentType() {

            return "application/json; charset=utf-8";

        }


        @Override

        public byte[] getBody() throws AuthFailureError {

            try {

                return requestBody == null ? null : requestBody.getBytes("utf-8");

            } catch (UnsupportedEncodingException uee) {

                VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", requestBody, "utf-8");

                return null;

            }  

5. 在您的 getHeaders() 方法中,將 Content-Type 用于“application/json”,對(duì)于授權(quán),您必須僅使用不帶 (Bearer) 的令牌。

完畢。


查看完整回答
反對(duì) 回復(fù) 2022-06-04
?
眼眸繁星

TA貢獻(xiàn)1873條經(jīng)驗(yàn) 獲得超9個(gè)贊

public void add(View view) throws JSONException {

   String URL = "http://10.0.2.2:8000/api/trucks";


   //removed views initialization from here.  

//you need to initialize views in oncreate() / oncreateView() method.  

/*first create json object in here.

then set keys as per required body format with values

*/

    JSONObject jsonObject = new JSONObject();

    jsonObject.put("Number", number.getText().toString());

    jsonObject.put("Make", make.getText().toString());

    jsonObject.put("Model", model.getText().toString());

    jsonObject.put("Description", description.getText().toString());

    jsonObject.put("Plates", plates.getText().toString());

    final String requestBody = jsonObject.toString();


 RequestQueue queue = Volley.newRequestQueue(this);


    StringRequest request = new StringRequest(Request.Method.POST, URL,

            new Response.Listener<String>() {

                @Override

                public void onResponse(String response) {

                    try {

                        JSONObject jsonObj = new JSONObject(response);

                        // parse response

                    } catch (JSONException e) {

                        e.printStackTrace();

                    }

                }

            },

            new Response.ErrorListener() {

                @Override

                public void onErrorResponse(VolleyError error) {

                    NetworkResponse response = error.networkResponse;

                    String errorMsg = "";

                    if (response != null && response.data != null) {

                        String errorString = new String(response.data);

                    }

                }

            }

    )  {    //this is the part, that adds the header to the request

//this is where you need to add the body related methods

@Override

    public String getBodyContentType() {

        return "application/json; charset=utf-8";

    }


    @Override

    public byte[] getBody() throws AuthFailureError {

        try {

            return requestBody == null ? null : requestBody.getBytes("utf-8");

        } catch (UnsupportedEncodingException uee) {

            VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s 

using %s", requestBody, "utf-8");

            return null;

        } 

        @Override

        public Map<String, String> getHeaders() {

            Map<String, String> params = new HashMap<String, String>();

            params.put("Content-Type", "application/json");

            params.put("Authorization", myToken);

            return params;

        }

    };

    queue.add(jsonRequest);

}  

在每個(gè)方法中輸入日志以檢查正在執(zhí)行的方法以及可能的錯(cuò)誤

如果在此之后有錯(cuò)誤,則從 logcat 中發(fā)布錯(cuò)誤,我們將迅速解決。


查看完整回答
反對(duì) 回復(fù) 2022-06-04
  • 3 回答
  • 0 關(guān)注
  • 156 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)