【发布时间】:2021-01-05 00:55:43
【问题描述】:
这是我从https://developer.android.com/training/volley/request#java填写的函数
String url = "http://192.168.1.31:8000/api/social/convert-token";
JSONObject jsonBody = new JSONObject();
try{
jsonBody.put("grant_type", "convert_token");
jsonBody.put("client_id", clientID);
jsonBody.put("client_secret",clientSecret);
jsonBody.put("backend", "facebook");
jsonBody.put("token", facebookAccessToken);
jsonBody.put("user_type", userType);
}catch (JSONException e){
e.printStackTrace();;
}
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
(Request.Method.POST, url, jsonBody, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
//Execute Code
Log.d("LOGIN TO SERVER", response.toString());
// Save server token to local DB
SharedPreferences.Editor editor = sharedPreferences.edit();
try {
editor.putString("token", response.getString("access_token"));
}catch (JSONException e){
e.printStackTrace();
}
editor.commit();
//Start home activity
startActivity(new Intent(Login.this,Home.class));
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO: Handle error
}
});
字符串 URL 是我的 Django 服务器,它工作正常,我可以从 android 手机上的网络浏览器访问它。
下一个块只是将我之前获得的数据转换为 JSON 格式。我通过复制和粘贴 URL 以及 Postman 中的每个参数对此进行了测试,它可以正常工作并返回 JSON。
jsonBody 然后与 URL 一起传递到 Json 请求中。 Log.d“登录到服务器”在我的日志中不可见。所以我知道 onResponse 没有运行。此外,我在 onErrorResponse 中添加了一条 Log 行,它在我的日志中可见。
所以 onReponse 不会运行,而 onError 响应会运行。我不知道错误是什么。
【问题讨论】:
标签: android django rest django-rest-framework android-volley