小编典典

JsonObjectRequest 发送空参数

all

我正在尝试将带有 json 参数的请求发送到我的节点后端。这是Android中的功能:

public boolean checkUser(String user, String passw) {

        final String url = "http://10.0.2.2:3000/";
        final String get = "auth";

        JSONObject paramJson = new JSONObject();

        try {
            paramJson.put("user", "value1");
            paramJson.put("pass", "value2");

        } catch (JSONException e) {
            e.printStackTrace();
        }

        Log.d("JSON", paramJson.toString());

        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET,url + get, paramJson,
                response -> {
                    Toast.makeText(getApplicationContext(),
                            response.toString(),
                            Toast.LENGTH_LONG);
                },
                error -> {
                    Toast.makeText(getApplicationContext(),
                            error.toString(),
                            Toast.LENGTH_LONG);
                });
        queue.add(jsonObjectRequest);
        return false;
    }

但是,当我尝试在后端获取 json 值时,我得到了一个空的正文。

app.get("/auth", async (req, res) => {
    let user = req.body.user;
    let pass = req.body.pass;
    console.log(req.body);
    if (!user || !pass) {
        res.status(500).send("Missing user or pass");
        return;
    }
    ...
}

所以基本上我不知道如何访问数据,我尝试记录整个请求,但我找不到 json 字段或值。这里有什么帮助吗?

编辑:

我已经尝试了以下方法:

CustomRequest.java

package com.example.apptonia;

import java.io.UnsupportedEncodingException;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.HttpHeaderParser;

public class CustomRequest extends Request<JSONObject> {

    private Listener<JSONObject> listener;
    private Map<String, String> params;

    public CustomRequest(String url, Map<String, String> params,
                         Listener<JSONObject> reponseListener, ErrorListener errorListener) {
        super(Method.GET, url, errorListener);
        this.listener = reponseListener;
        this.params = params;
    }

    public CustomRequest(int method, String url, Map<String, String> params,
                         Listener<JSONObject> reponseListener, ErrorListener errorListener) {
        super(method, url, errorListener);
        this.listener = reponseListener;
        this.params = params;
    }

    protected Map<String, String> getParams()
            throws com.android.volley.AuthFailureError {
        return params;
    };

    @Override
    protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
        try {
            String jsonString = new String(response.data,
                    HttpHeaderParser.parseCharset(response.headers));
            return Response.success(new JSONObject(jsonString),
                    HttpHeaderParser.parseCacheHeaders(response));
        } catch (UnsupportedEncodingException e) {
            return Response.error(new ParseError(e));
        } catch (JSONException je) {
            return Response.error(new ParseError(je));
        }
    }

    @Override
    protected void deliverResponse(JSONObject response) {
        // TODO Auto-generated method stub
        listener.onResponse(response);
    }
}

MainActivity.java

公共布尔检查用户(字符串用户,字符串密码){

final String url = "http://10.0.2.2:3000/";
final String get = "auth";

JSONObject paramJson = new JSONObject();

try {
    paramJson.put("user", "value1");
    paramJson.put("pass", "value2");

} catch (JSONException e) {
    e.printStackTrace();
}

Log.d("JSON", paramJson.toString());

Map<String, String> paramMap = new HashMap<String, String>();
paramMap.put("user", "value1");
paramMap.put("pass", "value2");

CustomRequest jsObjRequest =
        new CustomRequest(Request.Method.GET, url + get, paramMap,
        response -> {
            Toast.makeText(getApplicationContext(),
                    response.toString(),
                    Toast.LENGTH_LONG);
        },
                error -> {
        Toast.makeText(getApplicationContext(),
                    error.toString(),
                    Toast.LENGTH_LONG);
                });

queue.add(jsObjRequest);
return false;

}

上下文:我正在开发一个带有可以对用户进行身份验证的 API 的演示项目,因此我正在发送一个带有参数用户/密码的 get 请求。如果对此有更充分的要求,请告诉我。这是我的第一次。


阅读 71

收藏
2022-05-07

共1个答案

小编典典

https://johncodeos.com/how-to-make-post-get-put-and-delete-requests-with-retrofit-using-kotlin/

如何使用带有字符串正文的 volley 发送 POST 请求?

https://www.itsalif.info/content/android-volley-tutorial-http-get-post-put

请按照上面的链接获取 post json 数据

2022-05-07