java - API 返回错误:“字段不能为空”
问题描述
我目前正在尝试与 Tidal API 交互,但遇到了一些麻烦。这是我的代码,我正在使用 Volley 库:
JSONObject pload = new JSONObject();
try {
pload.put("username", username);
pload.put("password", password);
} catch (JSONException e) {
e.printStackTrace();
}
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, ENDPOINT, pload, response->{
Gson gson = new Gson();
JSONArray data = response.optJSONArray("data");
Log.d("RESPONSE", response.toString());
}, error -> {
String responseBody = null;
try {
responseBody = new String(error.networkResponse.data, "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
Log.d("ERROR", responseBody);
}){
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<>();
headers.put("X-Tidal-Token", "q--------------------------k");
headers.put("Content-Type", "application/json");
return headers;
}
};
mqueue.add(request);
日志猫:
D/REQUEST: body: {"username":"b-------@gmail.com","password":"p------"} ___ headers: {X-Tidal-Token=q---------------k, Content-Type=application/json} ___ request.tostring: [ ] https://api.tidalhifi.com/v1/login/username 0xcc20303d NORMAL null
E/Volley: [309] BasicNetwork.performRequest: Unexpected response code 400 for https://api.tidalhifi.com/v1/login/username
D/ERROR: {"status":400,"subStatus":1002,"userMessage":"password cannot be blank,username cannot be blank"}
如您所见,有效载荷不是空的,所以我有点困惑。Tidal 没有官方 API,但有一些我一直在使用的非官方包装器供参考,以下是一些使用代码的示例:
Javascript:
request({
method: 'POST',
uri: '/login/username?token=kgsOOmYk3zShYrNP',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
form: {
username: authInfo.username,
password: authInfo.password,
}
爪哇:
var url = baseUrl.newBuilder()
.addPathSegment("login")
.addPathSegment("username")
.build();
var body = new FormBody.Builder()
.add("username", username)
.add("password", password)
.build();
var req = new Request.Builder()
.url(url)
.post(body)
.header(TOKEN_HEADER, token)
.build();
再次Java:
HttpResponse<JsonNode> jsonResponse = restHelper.executeRequest(Unirest.post(API_URL + "login/username")
.header("X-Tidal-Token", "wdgaB1CilGA-S_s2")
.field("username", username)
.field("password", password));
如果需要,我可以发布所有包装器的链接并提供用于测试的潮汐令牌(很容易获得,您只需要从潮汐桌面应用程序中嗅探数据包)。我试过覆盖 getParams() 但这没有用。任何帮助表示赞赏!
解决方案
您使用错误的格式发送请求,当您需要 Url 编码格式时,您正在使用 Json,参考链接:Send form-urlencoded parameters in post request android volley 一种解决方案:字符串请求:
final String api = "http://api.url";
final StringRequest stringReq = new StringRequest(Request.Method.POST, api, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Toast.makeText(getApplicationContext(), "Login Successful!", Toast.LENGTH_LONG).show();
//do other things with the received JSONObject
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), "Error!", Toast.LENGTH_LONG).show();
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> pars = new HashMap<String, String>();
pars.put("Content-Type", "application/x-www-form-urlencoded");
return pars;
}
@Override
public Map<String, String> getParams() throws AuthFailureError {
Map<String, String> pars = new HashMap<String, String>();
pars.put("Username", "usr");
pars.put("Password", "passwd");
pars.put("grant_type", "password");
return pars;
}
};
//add to the request queue
requestqueue.AddToRequestQueue(stringReq);
推荐阅读
- python - PDF文件中的tkinter Python选择区域
- php - Laravel Passport 登录控制器单元功能测试
- python - 使用python将目录推送到bitbucket存储库
- amazon-web-services - 多容器部署到 ElasticBeanstalk
- python - 如何阻止从列表中输入数字?
- java - 通过映射表使用@ManyToMany 映射实体设置投影
- python - 检查字符串是否具有相同频率的字符,是否删除 1 个字符
- python - 使用 cx_Freeze 将 python 文件转换为可执行文件然后得到错误 AttributeError: 'list' object has no attribute 'main_script'
- mysql - 错误:ER_NOT_SUPPORTED_AUTH_MODE 节点docker镜像和本地mysql连接错误
- angular - Angular SSR 如何使用 canActive Auth 防护