带有标头和正文参数的排球请求

我需要提出一个 api 请求。

两个标题:

  1. 接受

  2. 授权

五个身体参数。

  1. 数字

  2. 制作

  3. 模型

  4. 描述

  5. 盘子

通过邮递员,一切都很好。但是当我尝试通过android应用程序时,我无法通过。注意:通过同一主机登录效果很好,所以设置不是问题,我认为我的主要问题在于 api 调用。

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

    }


HUWWW
浏览 146回答 3
3回答

MMTTMM

通过解决方案#1。public void add(View view) throws JSONException {&nbsp; &nbsp; RequestQueue queue = Volley.newRequestQueue(this);&nbsp; &nbsp; TextInputEditText number = findViewById(R.id.textInputEditTextNumber);&nbsp; &nbsp; TextInputEditText make = findViewById(R.id.textInputEditTextMake);&nbsp; &nbsp; TextInputEditText model = findViewById(R.id.textInputEditTextModel);&nbsp; &nbsp; TextInputEditText description = findViewById(R.id.textInputEditTextDescription);&nbsp; &nbsp; TextInputEditText plates = findViewById(R.id.textInputEditTextPlates);&nbsp; &nbsp; JSONObject jsonObject = new JSONObject();&nbsp; &nbsp; jsonObject.put("Number", number.getText().toString());&nbsp; &nbsp; jsonObject.put("Make", make.getText().toString());&nbsp; &nbsp; jsonObject.put("Model", model.getText().toString());&nbsp; &nbsp; jsonObject.put("Description", description.getText().toString());&nbsp; &nbsp; jsonObject.put("Plates", plates.getText().toString());&nbsp; &nbsp; final String requestBody = jsonObject.toString();&nbsp; &nbsp; JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.POST, "http://10.0.2.2:8000/api/trucks", jsonObject,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Response.Listener<JSONObject>() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onResponse(JSONObject response) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //now handle the response&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(truck_add.this, response.toString(), Toast.LENGTH_SHORT).show();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }, new Response.ErrorListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onErrorResponse(VolleyError error) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //handle the error&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(truck_add.this, "An error occurred", Toast.LENGTH_SHORT).show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; error.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }) {&nbsp; &nbsp; //this is the part, that adds the header to the request&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public Map<String, String> getHeaders() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Map<String, String> params = new HashMap<String, String>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; params.put("Accept", "application/json");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; params.put("Authorization", myToken);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return params;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; };&nbsp; &nbsp; queue.add(jsonRequest);}

qq_遁去的一_1

当你想通过 body 传递数据时,你需要在字符串请求之前创建一个 json 对象。试试这种方式,1. 为请求创建一个字符串 url。2.为body数据创建json对象并将数据传递给它。喜欢,JSONObject jsonObject= new JSONObject();&nbsp;&nbsp;jsonObject.put("Number", address.getNumber());&nbsp;&nbsp;jsonObject.put("Make", address.getMake());&nbsp;&nbsp;jsonObject.put("Model", address.getModel());&nbsp;&nbsp;jsonObject.put("Description", address.getDescription());&nbsp;&nbsp;jsonObject.put("Plates", address.getPlates());&nbsp;&nbsp;final String requestBody=jsonObject.toString();&nbsp;&nbsp;3. 在此之后,应用 stringRequest。4. 现在,在标题方法之前和错误方法之后添加以下行。@Override&nbsp; &nbsp; &nbsp; &nbsp; public String getBodyContentType() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return "application/json; charset=utf-8";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public byte[] getBody() throws AuthFailureError {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return requestBody == null ? null : requestBody.getBytes("utf-8");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (UnsupportedEncodingException uee) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", requestBody, "utf-8");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp;5. 在您的 getHeaders() 方法中,将 Content-Type 用于“application/json”,对于授权,您必须仅使用不带 (Bearer) 的令牌。完毕。

眼眸繁星

public void add(View view) throws JSONException {&nbsp; &nbsp;String URL = "http://10.0.2.2:8000/api/trucks";&nbsp; &nbsp;//removed views initialization from here.&nbsp;&nbsp;//you need to initialize views in oncreate() / oncreateView() method.&nbsp;&nbsp;/*first create json object in here.then set keys as per required body format with values*/&nbsp; &nbsp; JSONObject jsonObject = new JSONObject();&nbsp; &nbsp; jsonObject.put("Number", number.getText().toString());&nbsp; &nbsp; jsonObject.put("Make", make.getText().toString());&nbsp; &nbsp; jsonObject.put("Model", model.getText().toString());&nbsp; &nbsp; jsonObject.put("Description", description.getText().toString());&nbsp; &nbsp; jsonObject.put("Plates", plates.getText().toString());&nbsp; &nbsp; final String requestBody = jsonObject.toString();&nbsp;RequestQueue queue = Volley.newRequestQueue(this);&nbsp; &nbsp; StringRequest request = new StringRequest(Request.Method.POST, URL,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Response.Listener<String>() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onResponse(String response) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONObject jsonObj = new JSONObject(response);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // parse response&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (JSONException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Response.ErrorListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onErrorResponse(VolleyError error) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NetworkResponse response = error.networkResponse;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String errorMsg = "";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (response != null && response.data != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String errorString = new String(response.data);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; )&nbsp; {&nbsp; &nbsp; //this is the part, that adds the header to the request//this is where you need to add the body related methods@Override&nbsp; &nbsp; public String getBodyContentType() {&nbsp; &nbsp; &nbsp; &nbsp; return "application/json; charset=utf-8";&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public byte[] getBody() throws AuthFailureError {&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return requestBody == null ? null : requestBody.getBytes("utf-8");&nbsp; &nbsp; &nbsp; &nbsp; } catch (UnsupportedEncodingException uee) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s&nbsp;using %s", requestBody, "utf-8");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public Map<String, String> getHeaders() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Map<String, String> params = new HashMap<String, String>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; params.put("Content-Type", "application/json");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; params.put("Authorization", myToken);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return params;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; };&nbsp; &nbsp; queue.add(jsonRequest);}&nbsp;&nbsp;在每个方法中输入日志以检查正在执行的方法以及可能的错误如果在此之后有错误,则从 logcat 中发布错误,我们将迅速解决。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java