android - Android Volley 发布请求 - 无法解析响应?
问题描述
出于某种原因,我无法让它工作。如果我在 reqbin 等其他测试站点上尝试,请求应该可以工作并以正确的格式返回数据。
这是安卓代码:
System.out.println("VOLLEY POST");
String postUrl = "***MY LOGIN URL***";
RequestQueue requestQueue = Volley.newRequestQueue(this);
JSONObject postData = new JSONObject();
try {
postData.put("email", "test@email.dk");
postData.put("password", "abc123");
} catch (JSONException e) {
e.printStackTrace();
}
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, postUrl, postData, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
System.out.println(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
requestQueue.add(jsonObjectRequest);
但是,当我尝试运行它时,出现此错误:
com.android.volley.ParseError: org.json.JSONException: Value <pre of type java.lang.String cannot be converted to JSONObject
当我尝试在像 reqbin 这样的网站上使用相同的参数运行完全相同的请求时,我得到了这个响应(实际上是有效的 JSON):
{
"currency": 97,
"email": "jeppe@mail.dk",
"id": 1
}
谁能帮我澄清一下?我的 Java 代码中有什么错误吗?我一直在为此挠头..
顺便说一句,我是 Android 编程的新手,我来自 web/iOS 背景。
非常感谢阅读!
解决方案
推荐阅读
- spring - Spring Boot:某个类的方法的参数 0 需要一个无法找到的某个其他类类型的 bean
- php - 从 Laravel 中的数据库中删除项目
- android - React Native 出现“SDK 位置未找到”android 错误
- html - 有谁知道官方的 Auto-Scroll HTML 代码?
- python - 如何从 Python 路由中调用发送响应后应该继续的任务?
- javascript - 验证后发布表单
- google-cloud-platform - 是否有任何 API 访问 Google 翻译的“您的意思是”功能?
- cors - 主页未加载
- authorization - Blazor wasm 托管'无法从配置/TestApp.Client 加载设置-(AuthenticationService.js)
- ionic-framework - Flurry Analytics 是否支持华为设备上没有 Playstore/GMS 服务的标志性应用程序?