You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Volley请求返回错误响应,Postman正常,求问题排查

Troubleshooting Volley vs Postman Request Discrepancy

Hey there, let's dig into why your Volley request is throwing a 1020 Duplicate key not allowed error while Postman works perfectly and returns the expected 1089 Activation Key sent response. The core issue here is that the server is receiving different requests from Volley vs Postman—let's break down how to identify and fix this:

1. First, Verify the Exact Request Payload

The 1020 error tells us the server thinks you're trying to register a duplicate entry, which means the data Volley is sending might not match what you're sending in Postman.

  • Check your logged payload: Your code prints dataObj.toString() in both success and error callbacks. Compare this output directly to the JSON body you're using in Postman. Pay close attention to:
    • Spelling of fields (e.g., is deviceMacAddress spelled the same way in Postman?)
    • Exact values: Is the hardcoded 90:B6:86:0D:CE:4F the same MAC address you're using in Postman?
    • Extra whitespace: Add .trim() to your username/password values to eliminate accidental spaces:
      object.put("username", username.getText().toString().trim());
      object.put("password", password.getText().toString().trim());
      
  • Use a proxy to inspect the actual request: Tools like Charles Proxy or Fiddler will let you capture the exact HTTP request Volley sends, so you can compare every detail (body, headers, parameters) to your working Postman request.

2. Check for Header Differences

Servers often use headers to validate requests, and even a small mismatch can cause unexpected behavior.

  • Compare Content-Type: Your Volley code sets Content-Type: application/json; charset=utf-8—double-check that Postman is using the exact same value (not just application/json without charset).
  • Add Postman-like headers: Postman automatically adds headers like User-Agent and Accept that Volley might not include. Try mirroring these in your getHeaders() method:
    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        HashMap<String, String> headers = new HashMap<String, String>();
        headers.put("Content-Type", "application/json; charset=utf-8");
        // Copy the User-Agent from your Postman request (find it in Postman's Headers tab)
        headers.put("User-Agent", "PostmanRuntime/7.32.3");
        headers.put("Accept", "*/*");
        return headers;
    }
    

3. Rule Out Accidental Duplicate Requests

It's easy to accidentally send two requests with Volley (e.g., if your button click listener isn't debounced):

  • Add a log to track request calls: Put a Log.d(TAG, "Sending registration request...") at the start of sendRegisterationReq() to see if it's being triggered multiple times in quick succession.
  • Ensure no duplicate queue additions: Double-check that you're not adding the same request to the queue more than once (your code uses setTag("myRequest")—you could cancel existing requests with that tag before adding a new one to prevent duplicates).

4. Check Server Logs (If Accessible)

If you have access to the server's logs, look for the requests coming from Volley vs Postman. The logs will show exactly what data the server is receiving, which will point directly to the discrepancy (e.g., a missing field, incorrect value, or invalid header).

Quick Test to Try First

Modify your Volley request to use exactly the same JSON body as your working Postman request (hardcode all values temporarily) and see if it returns the 1089 response. If it does, the issue is with how you're constructing dataObj in your code; if not, the problem is with headers or request metadata.


内容的提问来源于stack exchange,提问作者Mushahid Gillani

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 06:56:59