You need to enable JavaScript to run this app. Android中使用Volley发送带路径参数POST请求的问题求助
优惠活动
大模型
产品
解决方案
定价
更多

Android中使用Volley发送带路径参数POST请求的问题求助

问题原因分析
  • 你当前实现的误区:getParams() 方法是用来提交POST请求的表单键值对参数,而你需要的/user/pass属于路径参数(Path Parameter),需要直接拼接在请求URL的末尾,不需要通过getParams()传递。
  • 你提供的Logcat报错和当前Volley请求逻辑无关,属于Google语音搜索服务的内部错误,不影响你的登录功能开发,可以忽略。
修改后的实现代码
// 实例化RequestQueue
RequestQueue queue = Volley.newRequestQueue(LoginActivity.this);
// 拼接路径参数到URL末尾,对用户名密码做URL编码避免特殊字符导致路径解析错误
String encodedUsername = URLEncoder.encode(username, "UTF-8");
String encodedPassword = URLEncoder.encode(password, "UTF-8");
String url = "http://localhost:8080/login/" + encodedUsername + "/" + encodedPassword;
// 发起POST请求
StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
        new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                System.out.println("USPESNO");
                try {
                    JSONObject objresponse = new JSONObject(response);
                    System.out.println(objresponse);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                Intent i = new Intent(LoginActivity.this,MainActivity.class);
                startActivity(i);
            }
        }, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        System.out.println("NEUSPESNO");
        // 原有代码遗漏了show()调用,补充后Toast才会正常弹出
        Toast.makeText(LoginActivity.this,"Pogresni podaci",Toast.LENGTH_LONG).show();
    }
});
// 将请求加入请求队列
queue.add(stringRequest);
补充注意事项
  • 如果你用安卓模拟器访问本地部署的后端服务,不要使用localhost作为请求地址,要替换为10.0.2.2,模拟器的localhost默认指向模拟器自身而非电脑本地环境。
  • URL编码步骤不可省略,避免用户名/密码中包含/、空格、特殊符号时导致请求路径识别错误。

内容的提问来源于stack exchange,提问作者Nikola Stankovic

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.13 15:42:02