我正在尝试使用Volley从Android调用服务器上的ASP.net Web api getTokens POST方法。
我从服务器收到400错误,我认为这是一个错误的请求。
我尝试了几种不同的方法,例如将content-type设置为
import numpy as np
for i in range(1,5):
data['result_' + str(i)] = np.maximum(data['a_' + str(i)] - data['b_' + str(i)], 0)
。 和
headers.put("Content-Type", "application/json; charset=utf-8");
但是我使用它并没有改变。我相信使用Android时应将其设置为application / json。
我在SO上看到过其他文章,其中提到getHeaders Override在某些情况下不会设置标头。
任何人都可以看到问题所在吗?
headers.put("Content-Type", "application/x-www-form-urlencoded");
。
try {
JSONObject js = new JSONObject();
try {
js.put("client_id", "HKTPEMS");
js.put("client_secret", "B83845-9AFDDD-46D7B487D");
js.put("grant_type", "password");
js.put("username", firstName);
js.put("password", password);
}catch (JSONException e) {
e.printStackTrace();
}
JsonObjectRequest jsonObjReq = new JsonObjectRequest(
Request.Method.POST, url, js, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.e(TAG, "response = " + response.toString());
iResult.notifySuccess(response);
ContentValues cv = new ContentValues();
String accessToken = null;
String refreshToken = null;
try {
//Get the accessToken
accessToken = (String) response.get("access_token");
Log.e(TAG, "accessToken = " + accessToken);
//Get the refreshToken
refreshToken = (String) response.get("refresh_token");
Log.e(TAG, "refreshToken = " + refreshToken);
}catch (Exception e){}
cv.putNull(LoginValidate.C_ID_TOKENS);
cv.put(LoginValidate.C_TOKENS_ACCESS_TOKEN, accessToken);
cv.put(LoginValidate.C_TOKENS_REFRESH_TOKEN, refreshToken);
appObj.loginValidate.insertIntoTokens(cv);
cvResult.put("resultcode", "OK");
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Error: " + error.getMessage());
Log.e(TAG, "Error stackTrace: " + error.getStackTrace().toString());
iResult.notifyError(error);
}
}) {
@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");
//headers.put("Content-Type", "application/x-www-form-urlencoded");
return headers;
}
};
RequestQueue queue = appObj.getRequestQueue();
queue.add(jsonObjReq);
} catch (Exception e) {
Log.e("json","e.getLocalizedMessage()"+ e.getLocalizedMessage());
cvResult.put("resultcode", "ERROR: problem getting the tokens");
}
[EDIT1]
/WebServicesForWebApi: inside WS getAuthenticationTokens()
E/LoginValidate: url already exists in DB, so returning it
E/WebServicesForWebApi: checking url for getting tokens. url = https://abc.xyz.co.uk/Token
E/LoginValidate: password already exists in DB, so returning it
E/LoginValidate: username already exists in DB, so returning it
E/WebServicesForWebApi: username = de4bcbc0bac3@XXXX.com
E/WebServicesForWebApi: password = 90661f6a-11c0-4bb3-b92a-cd9e38dfd229
E/WebServicesForWebApi: tokenCountInDB = 0
E/InstallationActivity: ContententsValues for tokens has size = 0
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
E/Volley: [55345] BasicNetwork.performRequest: Unexpected response code 400 for https://abc.xyz.co.uk/Token
E/WebServicesForWebApi: Error: null
E/WebServicesForWebApi: Error stackTrace: [Ljava.lang.StackTraceElement;@33976bc
E/InstallationActivity: ERROR for getAuthenticationTokens() = com.android.volley.ServerError