我正在将身份验证令牌呼叫发送到Rest-Api网址。当我发送呼叫时,它给了我这个错误
“ com.android.volley.ParseError:org.json.JSONException:值类型为java.lang.String的��������������͎�0���W�|NV��!转换为JSONObject“
这里是我发送呼叫的代码
String tokenUrl = "https://api.webname.com/oauth/token?username=username&password=password&grant_type=password";
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
(Request.Method.POST, tokenUrl, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
//stuff goes here
} catch (JSONException e) {
e.printStackTrace();
}
//Log.i("Response", response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i("Response", error.toString());
}
}) {
// * Passing some request headers*
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap();
headers.put("cache-control", "no-cache");
headers.put("accept", "*");
// I have alos tried headers.put("Content-Type", "application/json");
headers.put("Content-Type", "application/x-www-form-urlencoded");
headers.put("charset", "utf-8");
headers.put("Authorization", "Basic Y2xpZW50OnNlY3JldA==");
headers.put("accept-encoding", "gzip, deflate");
return headers;
}
};
我在网上搜索过,但是如果我重复他的问题,找不到任何相关的问题,对不起
添加依赖项:
实现'com.android.volley:volley:1.1.0'
使用下面的代码片段调用API:
private void APICall() {
// TODO: Implement this method to send token to your app server.
Common.ProgressDialogShow(context, getString(R.string.please_wait));
Map<String, String> params = new HashMap<String, String>();
params.put("username", "username");
params.put("password", "password");
params.put("grant_type", "password");
JsonObjectRequest request = new JsonObjectRequest("https://api.webname.com/oauth/token", new JSONObject(params),
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
parseJsonResponse(response);
} catch (Exception e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
if (error.networkResponse != null) {
int statusCode = error.networkResponse.statusCode;
NetworkResponse response = error.networkResponse;
Log.d("testerror", "" + statusCode + " " + response.data);
}
}
}) {
@Override
public Map<String, String> getHeaders() {
Map<String, String> headers = new HashMap<String, String>();
headers.put("User-agent", "Mozilla/5.0 (TV; rv:44.0) Gecko/44.0 Firefox/44.0");
return headers;
}
};
Common.setVolleyConnectionTimeout(request);
ApplicationClass.getInstance().getRequestQueue().add(request);
}
/**
* <b>Description</b> - Get back response for calling APICall API
*
* @param jsonObject - Pass API response
*/
private void parseJsonResponse(JSONObject jsonObject) {
try {
Common.ProgressDialogDismiss();
Log.i("get response", "get response" + jsonObject);
String access_token = jsonObject.getString("access_token");
String token_type = jsonObject.getString("token_type");
String refresh_token = jsonObject.getString("refresh_token");
String expires_in = jsonObject.getString("expires_in");
String scope = jsonObject.getString("scope");
String jti = jsonObject.getString("jti");
Log.d(TAG, "access_token: " + access_token);
Log.d(TAG, "token_type: " + token_type);
Log.d(TAG, "refresh_token: " + refresh_token);
Log.d(TAG, "expires_in: " + expires_in);
Log.d(TAG, "scope: " + scope);
Log.d(TAG, "jti: " + jti);
} catch (Exception e) {
e.getStackTrace();
}
}
您说过可以在仿真器中正常运行并在真实设备中运行,然后在以下链接中查看:
Soluion-1Soluion-2Soluion-3Soluion-4
此代码可以帮助您。.